sql server - Sqlserver group by pending days period -


i have table structure below,

state   application_count   pending_days _________________________________________ tn            10               0 tn            20               1 tn            60               2 tn            10               3 mh            40               1 mh            50               3 mh            20               5 mh            30               8 

i want sum application_count based on state , pending_days period. have group pending_days 0 1 days, 1 3 days, morethan 3 days

expected output:

state   application_count    _________________________    tn         30   tn         70   mh         40   mh         50   mh         50 

give additional column, group_num using case expression based on condition of pending_days column.

then find sum group group_num , state columns.

query

select t.[state], sum(t.[application_count]) [application_count] from(     select [group_num] = (         case when [pending_days] between 0 , 1 1          when [pending_days] between 2 , 3 2         when [pending_days] > 3 3 else 4 end     ), *      [your_table_name] )t group t.[group_num], t.[state]; 

find demo here

note:

for pending_days condition, 0 1 days have taken 0 , 1 1 3 days have taken 2 , 3 , morethan 3 days have taken value greater 3.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -