SQL按日和按用户查找记录计数

时间:2013-08-21 20:49:14

标签: sql count where-clause having-clause datepart

我正在尝试查找人员列出的天数,他们在录音表中有超过100条记录。它有条款问题,但我不知道如何区分人数。 where子句也有问题,我也试过把“Where(Recordings.ID)> 100”放在哪里,但也没用。以下是我到目前为止的情况:

SELECT Person.FirstName,
       Person.LastName,
       Count(Recordings.ID) AS DAYS_ABOVE_100
FROM   Recordings
JOIN   Person ON Recordings.PersonID=Person.ID
WHERE  DAYS_ABOVE_100 > 100
AND    Created BETWEEN '2013-08-01 00:00:00.000' AND '2013-08-21 00:00:00.000'
GROUP  BY Person.FirstName,
       Person.LastName
HAVING Count(DISTINCT PersonID), Count(Distinct Datepart(day, created))
ORDER  BY DAYS_ABOVE_100 DESC

我想要获得的示例数据:

First  Last    Days_Above_100
John   Doe     5
Jim    Smith   12

这意味着在给定时间范围内的5天内,John Doe每天有超过100条记录。

4 个答案:

答案 0 :(得分:1)

为了便于阅读,我将把问题分成两部分。

首先,弄清楚每个人每天录制的录音数量。这是公用表表达式(第一个select语句)中的查询。然后根据公用表表达式选择,将行限制为仅需要的行。

with cteRecordingsByDate as
(
  SELECT Person.FirstName,
        Person.LastName,
        cast(created as date) as Whole_date,
        Count(Recordings.ID) AS Recording_COUNT
  FROM   Recordings
  JOIN   Person ON Recordings.PersonID=Person.ID
  WHERE  Created BETWEEN '2013-08-01 00:00:00.000' AND '2013-08-21 00:00:00.000'
  GROUP  BY  Person.FirstName, Person.LastName, cast(created as date)
)

select FirstName, LastName, count(*) as Days_Above_100
from cteRecordingsByDate
where Recording_COUNT > 100
order by count(*) desc

答案 1 :(得分:0)

您可以使用子查询计算您想要的内容。内部查询计算每天的记录数。外子查询然后计算超过100的天数(并添加人员信息):

SELECT p.FirstName, p.LastName,
       count(*) as DaysOver100
FROM (select PersonId, cast(Created as Date) as thedate, count(*) as NumRecordings
      from Recordings r
      where Created BETWEEN '2013-08-01 00:00:00.000' AND '2013-08-21 00:00:00.000'
     ) r join
     Person p
     ON r.PersonID = p.ID
WHERE r.NumRecordings > 100
GROUP BY p.FirstName, p.LastName;

这使用SQL Server语法进行从datetimedate的转换。在其他数据库中,您可以使用trunc(created)date(created)从日期时间中提取日期。

答案 2 :(得分:0)

你应该试试这个:

 SELECT SUBQUERY.FirstName,
        SUBQUERY.LastName,
        Count(*) AS DAYS_ABOVE_100
 FROM
 (
     SELECT Person.FirstName,
            Person.LastName,
            Count(Recordings.ID) AS COUNT_RECORDINGS
     FROM   Recordings
     JOIN   Person ON Recordings.PersonID=Person.ID
     WHERE  Created BETWEEN '2013-08-01 00:00:00.000' AND 
                            '2013-08-21 00:00:00.000'
     GROUP  BY Person.FirstName,
               Person.LastName,
               Created 
     HAVING Count(Recordings.ID) > 100
 )SUBQUERY
 GROUP  BY SUBQUERY.FirstName,
           SUBQUERY.LastName
 ORDER  BY Count(*) DESC

认为Having子句的作用类似于Where子句,但是接受聚合的子句,如sum(source)。您的查询有一些错误,即:

  • WHERE 子句中的聚合,但Where子句不接受聚合。
  • 您在 ORDER BY 子句中使用别名“DAYS_ABOVE_100”来引用聚合“Count(Recordings.ID)”。您无法在定义该别名的同一选择内通过别名访问列。列只能通过查询外部的别名访问,就像Charles的回答一样。

修改

抱歉,我没有注意到“by day”部分,编辑了查询。将已创建的列添加到 GROUP BY 子句以获取每天的总记录数,然后将其包装在另一个查询中以计算结果,再次按FirstName和Lastname进行分组。

答案 3 :(得分:0)

我认为这就是你所追求的:

SELECT p.FirstName,
       p.LastName,
       COUNT(*) AS DAYS_ABOVE_100
FROM   (SELECT PersonID, Created, COUNT(*)
        FROM Recordings
        GROUP BY ID,Created
        HAVING COUNT(*) > 100
        )r
JOIN   Person p
  ON r.PersonID = p.ID
WHERE  Created BETWEEN '2013-08-01 00:00:00.000' AND '2013-08-21 00:00:00.000'
GROUP  BY p.FirstName,
          p.LastName
ORDER  BY DAYS_ABOVE_100 DESC