I'm working on a project, where we need to count the number of distinct rows. A simplified version of the scenario includes a user
table, a keyword
table and a keyword_user
table.
The user
table just includes common user meta data, like name etc. The other tables are listed below.
keyword_user:
id
user_id
keyword_id
keyword:
id,
description
What I want to do, is to find a max number of users (5), based on the users keyword_id's, while also counting the total number of matching rows. The count must be distinct.
The query:
SELECT TOP 5 u.[id],
u.[firstname],
u.[lastname],
total = Count(*) OVER()
FROM [user] u
INNER JOIN [keyword_user] ku
ON u.[id] = ku.[user_id]
WHERE ( ku.keyword_id IN ( '5f6501ec-0a71-4067-a21d-3c5f87a76411', 'c19b95c0-8554-4bbd-9526-db8f1c4f1edf'))
AND u.id NOT IN ( '12db3001-b3b9-4626-8a02-2519102cb53a' )
Resultset:
+--------------------------------------+-----------+----------+-------+
| id | firstname | lastname | total |
+--------------------------------------+-----------+----------+-------+
| F0527AC3-747A-45A6-9CF9-B1F6C7F548F8 | Kasper | Thomsen | 3 |
| 95988F6D-9C91-4779-B6C3-3D4B4D6AE836 | Michael | Jacobsen | 3 |
| 95988F6D-9C91-4779-B6C3-3D4B4D6AE836 | Michael | Jacobsen | 3 |
+--------------------------------------+-----------+----------+-------+
PROBLEM:
The problem being here, that Michael is counted twice, and the total count therefore is 3, when i want it to be 2. When using count() over()
you cannot parse an expression into it, that contains distinct. Also, if I just SELECT DISTINCT
, my resultset looks fine, besides the total count, which would still be 3.
If I need to include more information to support the question, please let me know, and I will try to answer the best I can.
MSSQL CREATE DB SCRIPT (SAMPLE DATA)
wanted resultset:
+--------------------------------------+-----------+----------+-------+
| id | firstname | lastname | total |
+--------------------------------------+-----------+----------+-------+
| F0527AC3-747A-45A6-9CF9-B1F6C7F548F8 | Kasper | Thomsen | 2 |
| 95988F6D-9C91-4779-B6C3-3D4B4D6AE836 | Michael | Jacobsen | 2 |
+--------------------------------------+-----------+----------+-------+