sql server - SQL grouped statement with count and percentage -
i have table(simplified)
id1 id2 status --- --- ------ 1 33 0 1 33 0 1 33 1 1 33 1 1 34 1 1 34 2 2 33 0 2 33 0 2 34 0
i want count total number of statuses grouped status type , percentage number of statuses pertaining particular ids id1 & id2
an example output be
id1 id2 status count percentage --- --- ------ ----- ---------- 1 33 0 2 50% 1 33 1 2 50% 1 34 1 1 50% 1 34 2 1 50% 2 33 0 2 100% 2 34 0 1 100%
so far have been able count, not percentage. query now
select id1, id2 , status, count(id2) count stattable group id1, id2, status
you can percentage using window functions total each id1
, id2
combination:
select id1, id2 , status, count(*) cnt, count(*) * 100.0 / count(*) on (partition id1, id2) percentage stattable group id1, id2, status;
Comments
Post a Comment