获取从当前日期开始的过去 12 个月的 SQL 中的结果集
Get Result set in SQL for the last 12 months starting from current date
所以我正在尝试从我的数据库中获取过去 12 个月的数据。我有一个非常大的存储过程,我首先在其中计算不依赖于时间跨度的数据。
首先,在我的 SP 上,我计算一个容器,我在其中获取一些数据,如年、月、id 等。其他计算基于这些数据(id、月、年)。
现在我的老板决定只计算过去 12 个月的数据。但我无法全神贯注。我正在考虑创建一个递归 CTE(通用 Table 表达式),我只计算过去 12 个月的时间。或许各位有不同的看法。让我向您展示我的起始查询。 :
SELECT DISTINCT Year, Month, BId, DCode, ParentB_ID AS [PId]
FROM [s].SK k
join Businesses b on b.id = k.BId
left join AD d on k.BId = d.DID
WHERE SName = 'New Test'
这个查询 return 告诉我这个:
Year Month BId DCode PId
---- ----- ---- ----- ---
2018 12 12 NULL NULL
2018 12 20 NULL 1
2018 12 33 NULL 26
2018 12 19 NULL 20
现在return应该只有年=2019年,月=3到现在的数据。
我正在尝试的是:
;WITH ACCC
AS
(
SELECT DISTINCT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM [s].SK k
join Businesses b on b.id = k.BId
left join AD d on k.BId = d.D_ID
WHERE SName = 'New Test'
//AND add here the logic for selecting data only for the last 12 months.
//Starting from current and ascending
CROSS JOIN
SELECT DISTINCT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM [s].SK s
INNER JOIN ACCC P
ON P.BId = s.D_ID
)
SELECT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM AC
我在这个 CTE 上走对了吗?还有别的办法吗?
我真的很难和这些人相处。任何帮助,我将不胜感激。
非常感谢您:)
如果您已经有了年份和月份,那么您可以像下面这样过滤过去 12 个月:
(这是 T-SQL/SQL 服务器)
IF OBJECT_ID('tempdb..#Foo') IS NOT NULL DROP TABLE #Foo;
CREATE TABLE #Foo (
[Year] INT NOT NULL,
[Month] INT NOT NULL,
Bar VARCHAR(20)
);
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2018, 1, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2018, 12, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 1, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 2, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 3, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 4, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 5, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 12, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 1, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 2, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 3, 'YES');
SELECT [Year], [Month], Bar
FROM #Foo
WHERE [Year] > YEAR(DATEADD(YEAR, -1, GETDATE()))
OR ([Year] = YEAR(DATEADD(YEAR, -1, GETDATE()))
AND [Month] >= MONTH(DATEADD(YEAR, -1, GETDATE())))
所以我正在尝试从我的数据库中获取过去 12 个月的数据。我有一个非常大的存储过程,我首先在其中计算不依赖于时间跨度的数据。 首先,在我的 SP 上,我计算一个容器,我在其中获取一些数据,如年、月、id 等。其他计算基于这些数据(id、月、年)。
现在我的老板决定只计算过去 12 个月的数据。但我无法全神贯注。我正在考虑创建一个递归 CTE(通用 Table 表达式),我只计算过去 12 个月的时间。或许各位有不同的看法。让我向您展示我的起始查询。 :
SELECT DISTINCT Year, Month, BId, DCode, ParentB_ID AS [PId]
FROM [s].SK k
join Businesses b on b.id = k.BId
left join AD d on k.BId = d.DID
WHERE SName = 'New Test'
这个查询 return 告诉我这个:
Year Month BId DCode PId
---- ----- ---- ----- ---
2018 12 12 NULL NULL
2018 12 20 NULL 1
2018 12 33 NULL 26
2018 12 19 NULL 20
现在return应该只有年=2019年,月=3到现在的数据。
我正在尝试的是:
;WITH ACCC
AS
(
SELECT DISTINCT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM [s].SK k
join Businesses b on b.id = k.BId
left join AD d on k.BId = d.D_ID
WHERE SName = 'New Test'
//AND add here the logic for selecting data only for the last 12 months.
//Starting from current and ascending
CROSS JOIN
SELECT DISTINCT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM [s].SK s
INNER JOIN ACCC P
ON P.BId = s.D_ID
)
SELECT Year, Month, BId, d.DCode, P_ID AS [ParentId]
FROM AC
我在这个 CTE 上走对了吗?还有别的办法吗?
我真的很难和这些人相处。任何帮助,我将不胜感激。 非常感谢您:)
如果您已经有了年份和月份,那么您可以像下面这样过滤过去 12 个月:
(这是 T-SQL/SQL 服务器)
IF OBJECT_ID('tempdb..#Foo') IS NOT NULL DROP TABLE #Foo;
CREATE TABLE #Foo (
[Year] INT NOT NULL,
[Month] INT NOT NULL,
Bar VARCHAR(20)
);
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2018, 1, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2018, 12, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 1, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 2, 'NO');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 3, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 4, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 5, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2019, 12, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 1, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 2, 'YES');
INSERT INTO #Foo([Year], [Month], Bar) VALUES (2020, 3, 'YES');
SELECT [Year], [Month], Bar
FROM #Foo
WHERE [Year] > YEAR(DATEADD(YEAR, -1, GETDATE()))
OR ([Year] = YEAR(DATEADD(YEAR, -1, GETDATE()))
AND [Month] >= MONTH(DATEADD(YEAR, -1, GETDATE())))