sql server - report sql query multiple wheres -
i trying write query in sql server grab these sample values:
firstname, lastname, count(reports status =1), count(reports status =2)
how these values in 1 sql statement?
here sample output
i think can accomplish pivot well. i'm not sure want to:
declare @tbl table(username varchar(10), report varchar(10), statusid int) insert @tbl values ('bob','report1',1) ,('bob','report2',1) ,('bob','report3',2) ,('jim','report2',1) ,('joe','report3',3) select username, [1] status1_count,[2] status2_count,[3] status3_count (select username ,statusid ,count(*) reportcount @tbl group username ,statusid) src pivot (max(statusid) statusid in ([1],[2],[3])) pvt
the group , count answer better in general, doesn't asked for, have status counts user on single 1 row.
Comments
Post a Comment