SQL SERVER: Count data with the same group using Partition -


enter image description here

based on picture above, need come countremarks column in count number of similar data in remarks column per location. so, tenant 1 , 2 have total of 2 in location "em" , total of 1 tenant 3 in same location. same counting location "cw".

unfortunately, have come output, counting same remnarks , disregarding location.

enter image description here

this portion in code used

 case         when [remarks]  >= 4 remarks          else count(remarks) over(partition [remarks] ) end 'countremark'   

try using window function , partition location , remarks:

select   location, tenant, remarks,   countremarks = count(*) on (partition location, remarks) your_table order tenant 

given sample data results in:

location    tenant  remarks countremarks em          tenant1 1       2 em          tenant2 1       2 em          tenant3 2       1 cw          tenant4 1       2 cw          tenant5 1       2 cw          tenant6 2       1 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -