SQL SERVER: Count data with the same group using Partition -
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.
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
Post a Comment