Skip to content
Advertisement

How do I add a column that counts unique instances of another columns data

I am an end user that does not work with SQL everyday. However I am sometimes asked to add modify existing queries to provide new data.

Currently I have a query that calculates (counts) the occurrences of a column labeled (Qid) and displays the top 5 based on count. It was necessary to use a select statement within a select statement to do this (I had to get some help)

I am being asked if it is possible to add a column that counts the unique instances of column entries “Scan_Run_Date_Week” for each of those associated top 5 QID. The new column is designed to display how many unique weeks the correlating QID has been present. (I Refer to it as “Time on Track”)

Attached below is a sample set of the data being worked with. How would I modify my existing query to include this newly desired column (Time_On_Track)

Kindest regards,

Runatyr

Raw Data

Existing Query Results

Advertisement

Answer

SQL Server doesn’t support count(distinct) as a window function, but there is a workaround using the sum of two dense_rank()s. So I think you want:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement