SQL筛选器查询列是否具有多个不同的值

时间:2018-11-05 09:47:34

标签: sql sql-server tsql group-by

我有一个查询,如果下载计数超过Date,则该查询返回用户的Username Download Count2

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

下面是返回的表

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

Jane下载了3次。

但是其中2个是同一文件(由fileNumber标识)。

如何更改查询,以免在该日期重复添加任何fileNumber的下载内容?

例如Jane一次下载了file1,两次下载了file2Download Count将计入2,而不是3

谢谢


编辑:在实现@ salman-a的答案之后:

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

让我知道是否误解了。谢谢。

3 个答案:

答案 0 :(得分:3)

在所有三个位置使用COUNT(DISTINCT fileNumber)

答案 1 :(得分:0)

使用fileNumber中的group by列并选择列表

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 

答案 2 :(得分:0)

希望这会有所帮助,通过在派生表中选择所需列的不同,可以获得所需的粒度级别。

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