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
Post a Comment