Sql 从类别中选择上周的前5名
我在MS SQL中有两个表:Sql 从类别中选择上周的前5名,sql,sql-server,Sql,Sql Server,我在MS SQL中有两个表: Category - Id (int) CategoryElement - Id (int) - CategoryId (int) //References Category.Id - Time (datetime) 因此,每个类别可以有零个或多个类别元素。CategoryElement中的时间表示创建category元素的时间 我需要一些帮助来编写以下查询:order categories按过去7天中添加的类别元素数降序,并显示类别id和添加的元素数
Category
- Id (int)
CategoryElement
- Id (int)
- CategoryId (int) //References Category.Id
- Time (datetime)
因此,每个类别可以有零个或多个类别元素。CategoryElement中的时间表示创建category元素的时间
我需要一些帮助来编写以下查询:order categories按过去7天中添加的类别元素数降序,并显示类别id和添加的元素数
到目前为止,我成功地编写了他们的查询,而没有在过去7天中添加以下内容:
SELECT c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
ORDER BY e.LatestElem DESC
看看下面的例子:
WHERE (Date(now()) - Date(Time)) < 7
看看下面的例子:
WHERE (Date(now()) - Date(Time)) < 7
您可以使用DATEDIFF和WHERE子句仅获取过去一周的数据,并使用TOP 5将结果限制为仅5行
SELECT TOP 5 c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
WHERE DATEDIFF(d, e.Time, GetDate()) < 7
ORDER BY e.LatestElem DESC
我上面的回答假设使用SQL Server。根据您的数据库类型,代码可能会有一些变化。例如,对于MySQL,如下所示:
SELECT c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
WHERE (Date(now()) - Date(e.Time)) < 7
ORDER BY e.LatestElem DESC
LIMIT 5
您可以使用DATEDIFF和WHERE子句仅获取过去一周的数据,并使用TOP 5将结果限制为仅5行
SELECT TOP 5 c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
WHERE DATEDIFF(d, e.Time, GetDate()) < 7
ORDER BY e.LatestElem DESC
我上面的回答假设使用SQL Server。根据您的数据库类型,代码可能会有一些变化。例如,对于MySQL,如下所示:
SELECT c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
WHERE (Date(now()) - Date(e.Time)) < 7
ORDER BY e.LatestElem DESC
LIMIT 5
不确定您正在使用什么RDBMS,但在SQL Server中,您可以执行以下操作:
SELECT c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
WHERE DATEDIFF(d, Time, CURRENT_TIMESTAMP) <= 7
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
ORDER BY e.LatestElem DESC
不确定您正在使用什么RDBMS,但在SQL Server中,您可以执行以下操作:
SELECT c.Id, e.ElemCount
FROM dbo.Categories AS c
INNER JOIN
(SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
WHERE DATEDIFF(d, Time, CURRENT_TIMESTAMP) <= 7
GROUP BY CategoryId) AS e
ON c.ID = e.CategoryId
ORDER BY e.LatestElem DESC
你最后7天的约束将进入你内在连接的子查询。像这样
SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
WHERE Time > dateadd(dd,-7,getDate()) -- 7 Days prior to today
GROUP BY CategoryId
你最后7天的约束将进入你内在连接的子查询。像这样
SELECT CategoryId, COUNT(1) AS ElemCount, MAX(Time) AS LatestElem
FROM dbo.CategoryElements
WHERE Time > dateadd(dd,-7,getDate()) -- 7 Days prior to today
GROUP BY CategoryId
您可以尝试此查询
SELECT CategoryId, COUNT(*) AS numbOfCat
FROM dbo.CategoryElement
WHERE [Time] BETWEEN DATEADD(day, -7, GETDATE()) AND GETDATE()
GROUP BY CategoryId
ORDER BY MAX(Time) DESC
演示
如果你需要前五名的话
SELECT TOP 5 CategoryId, COUNT(*) AS numbOfCat
FROM dbo.CategoryElement
WHERE [Time] BETWEEN DATEADD(day, -7, GETDATE()) AND GETDATE()
GROUP BY CategoryId
ORDER BY MAX(Time) DESC
您可以尝试此查询
SELECT CategoryId, COUNT(*) AS numbOfCat
FROM dbo.CategoryElement
WHERE [Time] BETWEEN DATEADD(day, -7, GETDATE()) AND GETDATE()
GROUP BY CategoryId
ORDER BY MAX(Time) DESC
演示
如果你需要前五名的话
SELECT TOP 5 CategoryId, COUNT(*) AS numbOfCat
FROM dbo.CategoryElement
WHERE [Time] BETWEEN DATEADD(day, -7, GETDATE()) AND GETDATE()
GROUP BY CategoryId
ORDER BY MAX(Time) DESC
您正在使用什么RDBMS?数据处理功能也会随之变化。您使用的是什么关系数据库?日期处理函数也会相应地变化。我认为DATEDIFF中的参数是向后的。它们很好理解。DATEDIFF定义为DATEDIFF datepart、startdate、enddate。我已经对我的答案做了必要的修改。谢谢你的回答。然而,它并没有给出正确的结果。最后的计数应该只包含上周添加的元素数…根据问题的标题,查询只返回5个结果。我认为DATEDIFF中的参数是反向的。它们是,很好的捕获。DATEDIFF定义为DATEDIFF datepart、startdate、enddate。我已经对我的答案做了必要的修改。谢谢你的回答。然而,它并没有给出正确的结果。最终计数应仅包含上周添加的元素数…根据问题的标题,查询仅返回5个结果。