function - Sum Rows based on a condition -


i'm using db2 database (not sure of version). script functional i'm not producing results expect. need separate aggregate totals in 3 groups based on conditions in case statement every project under same project_name/id , building_name/id, need sum total under each bucket having 1 row per project_name/id , building_name/id. assume group of sort or recursive function solution i'm not quite sure. appreciate push in right direction. here script.

    select         dp.dim_project_id,         dp.project_name,         dm.dim_building_id,         dm.building_name,         case when ( dj.groups3 in ('33.3% <= x <= 100%', '16.7% <= x < 33.3%') , da.type_name = 'sales')           cast(sum(cast(fat.transaction_amount real)) integer)          when (dj.groups3 in ('60% <= x <= 100%', '20% <= x < 60%') , da.type_name = 'sales')         cast(sum(cast(fat.transaction_amount real)) integer)          else '0'             end capability,               case when (dj.groups3 = '0% <= x < 16.7%' , da.type_name = 'sales')         cast(sum(cast(fat.transaction_amount real)) integer)          when (dj.groups3 = '0% <= x < 20%' , da.type_name = 'sales')         cast(sum(cast(fat.transaction_amount real)) integer)          else '0'                     end group_1,          case when (dj.groups3 = '16.7% <= x < 33.3%' , da.type_name = 'sales')         cast(sum(cast(fat.transaction_amount real)) integer)          when (dj.groups3 = '20% <= x < 60%' , da.type_name = 'sales')          cast(sum(cast(fat.transaction_amount real)) integer)              else '0'                 end group_2,         case when (dj.groups3 = '33.3% <= x <= 100%' , da.type_name = 'sales')          cast(sum(cast(fat.transaction_amount real)) integer)          when (dj.groups3 = '60% <= x <= 100%' , da.type_name = 'sales')          cast(sum(cast(fat.transaction_amount real)) integer)         else '0'                     end group_3 fact_table fat right join dim_allocation da on fat.dim_allocation_id = da.dim_allocation_id inner join dim_project dp on fat.dim_project_id = dp.dim_project_id inner join dim_date dd on fat.allocation_start_date_dim_id = dd.date_key inner join dim_job dj on fat.dim_job_id = dj.dim_job_id inner join dim_building dm on fat.dim_building_id = dm.dim_building_id      dd.date_value = '2013'     , da.building_name in ('admin', 'invision')   group dm.dim_building_id,          dp.dim_project_id,           dp.project_name,          capability,          dm.building_name,           dj.groups3,          da.type_name order dp.project_name 

results: enter image description here

desired results: enter image description here

i found answer , have posted below.

cast(sum(case when dj.groups3 in ('33.3% <= x <= 100%', '16.7% <= x < 33.3%')     cast(fat.transaction_amount real)     when dj.groups3 in ('60% <= x <= 100%', '20% <= x < 60%')     cast(fat.transaction_amount real)     else null     end)) integer)  capability, 

you take sum , cast outside case statement.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -