以下是可能對您有幫助的不同查詢。我假設數據存在於不同時間段的所有地點。
1]數據爲週一和上週一
SELECT
T1.[LocationName],
Monday,
LastMonday,
LastMonday-Monday AS [Difference]
FROM
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as Monday
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived = '2016-05-23'
GROUP BY ROLLUP(LI.LocationName)
) AS T1
LEFT JOIN
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as LastMonday
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived = '2016-05-16'
GROUP BY ROLLUP(LI.LocationName)
) AS T2
ON T1.LocationName=T2.LocationName
ORDER By T1.[LocationName]
2]數據周初至今上週迄今
DECLARE @LastWeekMonday DATE =DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)
DECLARE @ThisWeekMonday DATE = DATEADD(week, DATEDIFF(day, 0, getdate())/7, 0)
DECLARE @CurrentDate DATE =GETDATE()
--SELECT @LastWeekMonday,@ThisWeekMonday, @CurrentDate
SELECT
T1.[LocationName],
WeekToDate,
LastWeekToDate,
LastWeekToDate-WeekToDate AS [Difference]
FROM
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as WeekToDate
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived BETWEEN @ThisWeekMonday AND @CurrentDate
GROUP BY ROLLUP(LI.LocationName)
) AS T1
LEFT JOIN
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as LastWeekToDate
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived BETWEEN @LastWeekMonday AND @CurrentDate
GROUP BY ROLLUP(LI.LocationName)
) AS T2
ON T1.LocationName=T2.LocationName
ORDER By T1.[LocationName]
3]數據月初至今與上月日期
DECLARE @LastMonthStart DATE =DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
DECLARE @CurrentMonthStart DATE = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
DECLARE @CurrentDate DATE =GETDATE()
SELECT @LastMonthStart,@CurrentMonthStart, @CurrentDate
SELECT
T1.[LocationName],
MonthToDate,
LastMonthToDate,
LastMonthToDate-MonthToDate AS [Difference]
FROM
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as MonthToDate
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived BETWEEN @CurrentMonthStart AND @CurrentDate
GROUP BY ROLLUP(LI.LocationName)
) AS T1
LEFT JOIN
(
SELECT
CASE
WHEN GROUPING([LocationName]) = 1
THEN 'Total'
ELSE [LocationName]
END [LocationName],
COUNT(OI.LocationID) as LastMonthToDate
FROM OrderInfo AS OI
LEFT JOIN LocationInfo AS LI
ON OI.LocationID = LI.LocationID
WHERE DateReceived BETWEEN @LastMonthStart AND @CurrentDate
GROUP BY ROLLUP(LI.LocationName)
) AS T2
ON T1.LocationName=T2.LocationName
ORDER By T1.[LocationName]
where DateReceived ='2016-05-23' 你想要的數據只是爲那天或整個星期,即從16日到23日? –