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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -