Using PIVOT in sql -


i have following typestatusdetails table. table has different typeids. each type can have max 4 statusids (or less i.e. eg. typeid 3 has 3 statusids).

recordcount shows no. of records specific statusid. (eg. 3 pending records type_1)

--------------------------------------------------------------- typeid    type      statusid     statusname    recordcount --------------------------------------------------------------- 1         type_1      1          pending         3 1         type_1      2         in process       2 1         type_1      3         completed        1 1         type_1      4         invalid          1 2         type_2      1          pending         4 2         type_2      2         in process       5 2         type_2      3         completed        6 2         type_2      4         invalid          1 3         type_3      1          pending         1 3         type_3      2        in process        1 3         type_3      3        completed         1 

i want result following table:

depending on each type, want display recordcount per status statusid.

-------------------------------------------------------------------------------- type type  status  pending   status  inprocess  status  completed status invalid         id          id     count       id     count      id      count     id     count                                                                  pending           inprocess          completed          invalid -------------------------------------------------------------------------------- 1  type_1     1       3        2         2         3         1       4       1 2  type_2     1       4        2         5         3         6       4       1 3  type_3     1       1        2         1         3         1       4       0 

i have used following query

select * (     select         typeid,type,statusname,recordcount     #typestatusdetails ) s pivot (     sum(recordcount)     statusname in ([pending],[in process],[completed],[invalid])  )as pvt 

but having trouble statusid column per each statusname combine in pivot.

can please me this?

you can using conditional aggregation instead of pivot:

select typeid, type,          max(case when statusname = 'pending' statusid end) [status pending],        sum(case when statusname = 'pending' recordcount else 0 end) [pending],        max(case when statusname = 'in process' statusid end) [status in process],        sum(case when statusname = 'in process' recordcount else 0 end) [in process],        max(case when statusname = 'completed' statusid end) [status completed],        sum(case when statusname = 'completed' recordcount else 0 end) [completed],        max(case when statusname = 'invalid' statusid end) [status invalid],        sum(case when statusname = 'invalid' recordcount else 0 end) [invalid] typestatusdetails group typeid, type 

the above query standard ansi sql , can used in rdmbs.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -