使用 SQL Server 2012 在查询中添加月初至今和年初至今?
Adding Month-to-date and Year-to-date in a query using SQL Server 2012?
所以我正在尝试进行查询,其中包括从数据库开始收集数据的第一个实例到该日期的最后一个可用实例(数据库每小时收集一次数据)的每日总和。虽然我已经这样做了,但现在我必须让它显示一个月至今和一年至今的总金额。我尝试了各种方法来解决这个问题,但没有运气。下面是我认为最接近实现此目的的代码。有人可以帮助我使我的代码正常工作或提出其他解决方法吗?
Select * from
(
SELECT Devices.DeviceDesc,
SUM(DeviceSummaryData.Amount) AS MTD,
Devices.Area,
MIN(DeviceSummaryData.StartDate) AS FirstOfStartDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate = MONTH(getdate())) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, GETDATE())
AS date))
GROUP BY Devices.DeviceDesc, Devices.Area, DATEPART(day, DeviceSummaryData.StartDate)
--
) q2
UNION ALL
SELECT * FROM (
SELECT Devices.DeviceDesc,
Sum(Amount) as Daily,
Devices.Area,
MIN(StartDate) as FirstDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, GETDATE()) AS date)) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, getdate()) AS date))
GROUP BY Devices.Area,
Devices.DeviceDesc,
DATEPART(day, DeviceSummaryData.StartDate)
ORDER BY Devices.DeviceDesc
) q2
我尝试过的另一种尝试是:
SELECT Devices.DeviceDesc,
Sum(case
when DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, getdate()) AS date)
THEN Amount
else 0
end) as Daily,
Sum(case
when Month(StartDate) = MONTH(getdate())
THEN Amount
else 0
end) as MTD,
Devices.Area,
MIN(StartDate) as FirstDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, GETDATE()) AS date)) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, getdate()) AS date))
GROUP BY Devices.Area,
Devices.DeviceDesc,
DATEPART(day, DeviceSummaryData.StartDate)
ORDER BY Devices.DeviceDesc
我不是 Case When 的最佳人选,但我在某处看到这是一种可行的方法。我不太关心速度或效率,我只需要它来生成能够获取数据的查询。非常感谢任何帮助和建议!
第二次尝试是对的,但有点迷茫。在 CASE
语句中,您试图比较月份等,但您的 WHERE
子句将您正在查看的数据限制为一天。此外,您的 GROUP BY
不应再包括这一天。如果你用英语说你想要什么,那就是"For each device area and type, I want to see a total, a MTD total and a YTD total"。 "For each" 位应该定义 GROUP BY
.
中出现的内容
只需完全删除 WHERE
子句并从 GROUP BY
中删除 DATEPART(day, DeviceSummaryData.StartDate)
,您应该会得到想要的结果。 (好吧,无论如何,每日和每月总计。每年的实现方式大致相同)。
另请注意 DATEADD(DAY, 0, GETDATE())
等同于 GETDATE()
。
所以我正在尝试进行查询,其中包括从数据库开始收集数据的第一个实例到该日期的最后一个可用实例(数据库每小时收集一次数据)的每日总和。虽然我已经这样做了,但现在我必须让它显示一个月至今和一年至今的总金额。我尝试了各种方法来解决这个问题,但没有运气。下面是我认为最接近实现此目的的代码。有人可以帮助我使我的代码正常工作或提出其他解决方法吗?
Select * from
(
SELECT Devices.DeviceDesc,
SUM(DeviceSummaryData.Amount) AS MTD,
Devices.Area,
MIN(DeviceSummaryData.StartDate) AS FirstOfStartDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate = MONTH(getdate())) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, GETDATE())
AS date))
GROUP BY Devices.DeviceDesc, Devices.Area, DATEPART(day, DeviceSummaryData.StartDate)
--
) q2
UNION ALL
SELECT * FROM (
SELECT Devices.DeviceDesc,
Sum(Amount) as Daily,
Devices.Area,
MIN(StartDate) as FirstDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, GETDATE()) AS date)) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, getdate()) AS date))
GROUP BY Devices.Area,
Devices.DeviceDesc,
DATEPART(day, DeviceSummaryData.StartDate)
ORDER BY Devices.DeviceDesc
) q2
我尝试过的另一种尝试是:
SELECT Devices.DeviceDesc,
Sum(case
when DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, getdate()) AS date)
THEN Amount
else 0
end) as Daily,
Sum(case
when Month(StartDate) = MONTH(getdate())
THEN Amount
else 0
end) as MTD,
Devices.Area,
MIN(StartDate) as FirstDate,
MAX(DeviceSummaryData.EndDate) AS LastOfStartDate
FROM Devices INNER JOIN DeviceSummaryData ON Devices.DeviceID = DeviceSummaryData.DeviceID
WHERE (DeviceSummaryData.StartDate >= CAST(DATEADD(DAY, 0, GETDATE()) AS date)) AND (DeviceSummaryData.EndDate <= CAST(DATEADD(DAY, 1, getdate()) AS date))
GROUP BY Devices.Area,
Devices.DeviceDesc,
DATEPART(day, DeviceSummaryData.StartDate)
ORDER BY Devices.DeviceDesc
我不是 Case When 的最佳人选,但我在某处看到这是一种可行的方法。我不太关心速度或效率,我只需要它来生成能够获取数据的查询。非常感谢任何帮助和建议!
第二次尝试是对的,但有点迷茫。在 CASE
语句中,您试图比较月份等,但您的 WHERE
子句将您正在查看的数据限制为一天。此外,您的 GROUP BY
不应再包括这一天。如果你用英语说你想要什么,那就是"For each device area and type, I want to see a total, a MTD total and a YTD total"。 "For each" 位应该定义 GROUP BY
.
只需完全删除 WHERE
子句并从 GROUP BY
中删除 DATEPART(day, DeviceSummaryData.StartDate)
,您应该会得到想要的结果。 (好吧,无论如何,每日和每月总计。每年的实现方式大致相同)。
另请注意 DATEADD(DAY, 0, GETDATE())
等同于 GETDATE()
。