sql - Combining groups in GROUP BY -


i have query groups field description. (there 9 total descriptions.) need somehow combine 3 of these descriptions 1 group, giving me total of 7 descriptions. i'm brand new sql querying, i'm lost on beyond joins , clauses far. appreciate help.

select        serviceclass.description, datepart(year, meterread.readdate) year, datepart(month, meterread.readdate) month, sum(meterread.consumption)                       consumption, count(distinct accountservice.account_id) locations            accountservice inner join                      serviceclass on accountservice.serviceclass_id = serviceclass.serviceclass_id inner join                      meterread on accountservice.accountservice_id = meterread.accountservice_id        (@year = datepart(year, meterread.readdate)) group serviceclass.description, datepart(year, meterread.readdate), datepart(month,meterread.readdate) order year, month 

you can achieve using case statement.

replace serviceclass.desription in select , group by clauses with

case     when serviceclass.description in ('desc1','desc2','desc3')         'jointdescription'     else serviceclass.description end 

and update desc 1 ~ 3 3 description , updated joint description want be.

result:

select case            when serviceclass.description in ('desc1','desc2','desc3')                'jointdescription'            else serviceclass.description        end description,         datepart(year, meterread.readdate) year,          datepart(month, meterread.readdate) month,         sum(meterread.consumption) consumption,         count(distinct accountservice.account_id) locations accountservice  inner join serviceclass on accountservice.serviceclass_id = serviceclass.serviceclass_id  inner join meterread on accountservice.accountservice_id = meterread.accountservice_id        (@year = datepart(year, meterread.readdate)) group case              when serviceclass.description in ('desc1','desc2','desc3')                   'jointdescription'              else serviceclass.description          end,           datepart(year, meterread.readdate),           datepart(month,meterread.readdate) order year, month 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -