gymcode
gymcode

Reputation: 4623

SQL Filter query if column has more than one distinct value

I have a query that returns Date Username and Download Count of a user if download counts exceeds 2.

SELECT CONVERT(DATE, [datetime]) 'Date', 
       actionby                  AS 'Username', 
       Count(*)                  'Download Count' 
FROM   [dbo].[log] 
WHERE  actiontaken = 'Download' 
       AND type = 'Document' 
       AND CONVERT(DATE, [datetime]) BETWEEN 
           CONVERT(DATE, 
           '" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
           AND 
CONVERT(DATE, '" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
GROUP  BY CONVERT(DATE, [datetime]), 
          actionby 
HAVING Count(*) > 1 
ORDER  BY Count(*) DESC

Below is the returned table

+------------+----------+----------------+
|    Date    | Username | Download Count |
+------------+----------+----------------+
| 01/01/2018 | Jane     |              3 |
+------------+----------+----------------+

Jane did 3 downloads.

But 2 of them are the same file (identified by fileNumber).

How can I change my query so that it will not add in duplicated downloads of any fileNumber on that date?

e.g. Jane downloaded file1 once, and file2 twice. Download Count will count 2 instead of 3.

Thank you


EDIT: After implementing @salman-a's answer:

SELECT CONVERT(DATE, [datetime])  'Date', 
       actionby                   'Username', 
       COUNT(DISTINCT filenumber) 'Download Count' 
FROM   [dbo].[log] 
WHERE  actiontaken = 'Download' 
       AND type = 'Document' 
       AND CONVERT(DATE, [datetime]) BETWEEN 
           CONVERT(DATE, 
           '" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
           AND 
CONVERT(DATE, '" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
GROUP  BY CONVERT(DATE, [datetime]), 
          actionby
HAVING COUNT(DISTINCT filenumber) > 1 
ORDER  BY COUNT(DISTINCT filenumber) DESC

Let me know if I misunderstood it. Thank you.

Upvotes: 0

Views: 705

Answers (3)

Fahmi
Fahmi

Reputation: 37473

Use fileNumber column in group by and select list

select [date],Username,[Download Count] 
from
(SELECT CONVERT(DATE, [datetime]) 'Date', 
           actionby                  AS 'Username', 
           filenumber, 
           Count(*)                  'Download Count' 
    FROM   [dbo].[log] 
    WHERE  actiontaken = 'Download' 
           AND type = 'Document' 
           AND CONVERT(DATE, [datetime]) BETWEEN 
               CONVERT(DATE, 
               '" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
               AND 
    CONVERT(DATE, '" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
    GROUP  BY CONVERT(DATE, [datetime]), 
              actionby, 
              filenumber )X
   where [Download Count]>1 
    ORDER  BY [Download Count] DESC 

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272006

Use COUNT(DISTINCT fileNumber) in all three places.

Upvotes: 3

Ajan Balakumaran
Ajan Balakumaran

Reputation: 1649

Hope this helps, by selecting the distinct of desired columns in a derived table you can get the desired granular level.

SELECT Date, ActionBy AS 'Username', COUNT(*) 'Download Count' 

( SELECT DISTINCT CONVERT(date, [DateTime]) 'Date',FileNumber,ActionBy,ActionTaken,Type 
FROM [dbo].[Log])a 

WHERE ActionTaken = 'Download' and Type = 'Document' and CONVERT(date, [DateTime]) 

BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 
and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') 

GROUP BY CONVERT(date, [DateTime]), ActionBy 

HAVING COUNT(*) > 1 

ORDER BY COUNT(*) DESC

Upvotes: 0

Related Questions