sql server - Get the count in a separate column in SQL -
so have following table:
ref| status op1|0 op2|2 op2|4 op2|5 op3|7 op3|6
as can see, there 3 statueses op2 , 2 statuses op3. trying archive following:
ref| status | count op1|0 |1 op2|2 |3 op2|4 |3 op2|5 |3 op3|7 |2 op3|6 |2
count being number of status each ref.
i have provided sql not sure going wrong?
select ref,status, (select count(status) orders status > 0 group ref) orders
i using sql server 2008.
use count() over()
select ref,status, count(*) over(partition ref1) orders
to count status > 0, above query can changed to
select ref,status, count(case when status>0 1 end) over(partition ref1) orders
Comments
Post a Comment