a query in sql server -


i have table field in sql server : user_id , seller_id , cat_id , brand_id , action_type

action_type values 0 , 1 , 2 , 3

i want query can show me per "user_id , seller id "

the number of 0 fileds action type

the number of 1 fileds action type

the number of 2 fileds action type

the number of 3 fileds action type

fo example :

user1 seller1 cat1 brand1 0

user1 seller1 cat1 brand1 0

user1 seller1 cat2 brand1 1

user1 seller1 cat2 brand1 0

user1 seller1 cat1 brand1 0

user1 seller1 cat3 brand1 2

user1 seller1 cat3 brand1 2

user1 seller1 cat4 brand1 2

i want table in output can show me number of action type: column of output table :

user_id seller_id , seller action_type_0 , action_type_1 ,action_type_2 action_type_3

output:

user1 seller1 4 1 3

description"

4:number of 0 in action_type per seller_id , user_id

1:number of 1 in action_type per seller_id , user_id

2:number of 2 in action_type per seller_id , user_id

i think looking for, others have commented, easier if posted have tried far.

select user_id, seller_id , count(case when action_type = 0 action_type end) type0 , count(case when action_type = 1 action_type end) type1 [...] table group user_id, seller_id 

Comments