0

我正在尝试查找在记录表中有超过 100 条记录的人列出的天数。拥有子句有问题,但我不确定如何按人区分计数。where 子句也有问题,我也尝试输入“where Count(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

4 回答 4

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
于 2013-08-21T21:14:40.457 回答
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 语法进行从datetime到的转换date。在其他数据库中,您可能会使用trunc(created)date(created)从日期时间中提取日期。

于 2013-08-21T20:53:06.673 回答
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
于 2013-08-21T21:10:20.987 回答
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

认为 Have 子句的工作方式类似于 Where 子句,但它接受聚合,例如 sum ( source )。您的查询有一些错误,即:

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

编辑:

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

于 2013-08-21T21:02:32.870 回答