SQL 查询列出最近 7 天的记录
SQL Query to list records for last 7 days
我需要查询列出该日期的所有记录,如果记录不存在则查询应列出 0
SELECT Count(C.ConversionStatusID) Visits, CONVERT(VARCHAR(10),ActionDate,110) ActionDate
FROM Conversion C
WHERE C.ConversionStatusID = 2 AND
ActionDate Between DateAdd(day,-7,GetDate()) AND GETDATE()
GROUP BY CONVERT(VARCHAR(10),ActionDate,110)
Order BY CONVERT(VARCHAR(10),ActionDate,110) DESC
预期输出应始终为 7 条记录,示例结果应如下所示
ActionDate Visits
01-09-2015 1
01-08-2015 5
01-07-2015 0
01-06-2015 0
01-05-2015 3
01-04-2015 8
01-03-2015 0
提前致谢
您需要有过去 7 天的日期 table,然后您可以 left join
with cte (value, n)
as
(
select DATEADD(DAY, DATEDIFF(day,0,getdate()),0) as value,1 as n
UNION ALL
SELECT DATEADD(day, -1, value) as value, n+1
from cte
where n < 7
)
select CONVERT(VARCHAR(10),cte.value,110) as ActionDate , Count(C.ConversionStatusID) Visits
from cte
left join Conversion C
ON CONVERT(VARCHAR(10),C.ActionDate,110) = CONVERT(VARCHAR(10),cte.value,110)
and C.ConversionStatusID = 2
GROUP BY CONVERT(VARCHAR(10),cte.value,110)
Order BY CONVERT(VARCHAR(10),cte.value,110) DESC
像这样的东西会起作用,但你将不得不考虑什么应该是日期,什么应该是日期时间。根本不清楚您需要时间组件的时间和地点。特别是,您的 WHERE
子句似乎与您的其余查询相矛盾,因为它没有占用时间。例如,如果 GETDATE()
是 1 月 12 日下午 2 点,那么 1 月 11 日下午 1 点是前一天还是前两天?那么,1 月 5 日下午 1 点怎么样,因为 WHERE
子句正在剥离它。
如果日期是 00:00 到 23:59(尽管 WHERE
子句仍然关闭):
;WITH Dates ([Date]) AS (
SELECT CAST(DATEADD(DAY,-1,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-2,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-3,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-4,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-5,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-6,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-7,GETDATE()) AS DATE))
SELECT D.Date,
COALESCE(Count(C.ConversionStatusID),0) Visits
FROM Dates D
LEFT JOIN Conversion C
ON D.[Date] = CAST(C.ActionDate AS Date)
WHERE C.ConversionStatusID = 2 AND
ActionDate Between DateAdd(day,-7,GetDate()) AND GETDATE()
GROUP BY CONVERT(VARCHAR(10),ActionDate,110)
Order BY CONVERT(VARCHAR(10),ActionDate,110) DESC
如果您有 Numbers 或 tally table,您可以不使用 CTE 而逃脱惩罚 table,但对于这么小的东西,它真的没有那么重要。
我需要查询列出该日期的所有记录,如果记录不存在则查询应列出 0
SELECT Count(C.ConversionStatusID) Visits, CONVERT(VARCHAR(10),ActionDate,110) ActionDate
FROM Conversion C
WHERE C.ConversionStatusID = 2 AND
ActionDate Between DateAdd(day,-7,GetDate()) AND GETDATE()
GROUP BY CONVERT(VARCHAR(10),ActionDate,110)
Order BY CONVERT(VARCHAR(10),ActionDate,110) DESC
预期输出应始终为 7 条记录,示例结果应如下所示
ActionDate Visits
01-09-2015 1
01-08-2015 5
01-07-2015 0
01-06-2015 0
01-05-2015 3
01-04-2015 8
01-03-2015 0
提前致谢
您需要有过去 7 天的日期 table,然后您可以 left join
with cte (value, n)
as
(
select DATEADD(DAY, DATEDIFF(day,0,getdate()),0) as value,1 as n
UNION ALL
SELECT DATEADD(day, -1, value) as value, n+1
from cte
where n < 7
)
select CONVERT(VARCHAR(10),cte.value,110) as ActionDate , Count(C.ConversionStatusID) Visits
from cte
left join Conversion C
ON CONVERT(VARCHAR(10),C.ActionDate,110) = CONVERT(VARCHAR(10),cte.value,110)
and C.ConversionStatusID = 2
GROUP BY CONVERT(VARCHAR(10),cte.value,110)
Order BY CONVERT(VARCHAR(10),cte.value,110) DESC
像这样的东西会起作用,但你将不得不考虑什么应该是日期,什么应该是日期时间。根本不清楚您需要时间组件的时间和地点。特别是,您的 WHERE
子句似乎与您的其余查询相矛盾,因为它没有占用时间。例如,如果 GETDATE()
是 1 月 12 日下午 2 点,那么 1 月 11 日下午 1 点是前一天还是前两天?那么,1 月 5 日下午 1 点怎么样,因为 WHERE
子句正在剥离它。
如果日期是 00:00 到 23:59(尽管 WHERE
子句仍然关闭):
;WITH Dates ([Date]) AS (
SELECT CAST(DATEADD(DAY,-1,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-2,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-3,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-4,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-5,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-6,GETDATE()) AS DATE) UNION
SELECT CAST(DATEADD(DAY,-7,GETDATE()) AS DATE))
SELECT D.Date,
COALESCE(Count(C.ConversionStatusID),0) Visits
FROM Dates D
LEFT JOIN Conversion C
ON D.[Date] = CAST(C.ActionDate AS Date)
WHERE C.ConversionStatusID = 2 AND
ActionDate Between DateAdd(day,-7,GetDate()) AND GETDATE()
GROUP BY CONVERT(VARCHAR(10),ActionDate,110)
Order BY CONVERT(VARCHAR(10),ActionDate,110) DESC
如果您有 Numbers 或 tally table,您可以不使用 CTE 而逃脱惩罚 table,但对于这么小的东西,它真的没有那么重要。