收到的样品总和,7天前收到的样品总和,两次总和的差值,总计在列底部

Sum of samples received, sum of samples received 7 days ago, difference between two sums with total at bottom of column

我想创建以下内容:

Location Name   Monday  Last Monday Delta
Location 2        90          92      -2
Location 4        76          71      5
Location 1        20          23      -3
Location 3        15          13      2
Total             201         199     2

来自 table OrderInfo

ID    LocationID    DateReceived  
1     1             2016-05-23
2     3             2016-05-23
...
100   1             2016-05-16
101   4             2016-05-15

带有 table 位置信息

ID    LocationName
1     Location 1
2     Location 2
3     Location 3
4     Location 4

我目前拥有的代码:

select 
case 
when grouping([LocationName]) = 1 
then 'Total' 
else [LocationName] 
end [LocationName], 
count(OrderInfo.LocationID) as Monday 
from OrderInfo 
left join LocationInfo 
on OrderInfo.LocationID = LocationInfo.LocationID
where DateReceived = '2016-05-23' 
group by rollup(LocationInfo.LocationName) 
order by count(OrderInfo.LocationID) desc

给出:

LocationName    Monday
Total           201
Location 2      90
Location 4      76
Location 1      20
Location 3      15

我可以重复将“2016-05-23”替换为“2016-05-16”的代码,但无法弄清楚如何连接两个查询、获取两个查询之间的差异并对结果进行排序总数在底部。

我也想创建具有相同格式的结果,但将星期一和上星期一替换为本周至今和上周至今,一个用于本月至今和上个月至今.

如有任何帮助,我们将不胜感激。

假设您的意思是您想要在星期一之前的整个星期的计数:

--Start: get the number of days since monday
DECLARE @dayname nvarchar(10) = DATENAME(dw,GETDATE())
        ,@dayscounter int = 0
WHILE @dayname <> 'Monday'
BEGIN
    SET @dayscounter = @dayscounter + 1
    SET @dayname = DATENAME(dw,DATEADD(dd,-@dayscounter,GETDATE()))
END
--End: get the number of days since monday
SELECT CASE WHEN (GROUPING(l.LocationName) = 1) THEN 'ALL' ELSE l.LocationName END
      ,ISNULL(SUM(CASE WHEN [DataSet] = 1 THEN 1 END),0) AS Monday
      ,ISNULL(SUM(CASE WHEN [DataSet] = 2 THEN 1 END),0) AS [Last Monday]
      ,ISNULL(SUM(CASE WHEN [DataSet] = 1 THEN 1 END),0)
       -ISNULL(SUM(CASE WHEN [DataSet] = 2 THEN 1 END),0) AS Delta
FROM (--This is the data for a week since last Monday with 1 AS Dataset
      --unioned with same for a week before that, 2 AS Dataset
      SELECT [ID],[LocationID],[DateReceived],1 AS [Dataset]
      FROM [MDSNordeaProd].[dbo].[OrderInfo]
      WHERE [DateReceived] BETWEEN CONVERT(date, DATEADD(dd,-(@dayscounter+6),GETDATE()))
            AND CONVERT(date, DATEADD(dd,-@dayscounter,GETDATE()))
        UNION
      SELECT [ID],[LocationID],[DateReceived],2 AS [Dataset]
      FROM [MDSNordeaProd].[dbo].[OrderInfo]
      WHERE [DateReceived] BETWEEN CONVERT(date, DATEADD(dd,-(@dayscounter+14),GETDATE()))
            AND CONVERT(date, DATEADD(dd,-(@dayscounter+7),GETDATE()))
      )AS o
INNER JOIN [LocationInfo] AS l ON o.LocationID=l.LocationID
GROUP BY l.LocationName WITH ROLLUP

如果您只想要星期一的计数,请像这样更改并集部分中的 WHERE 条件:

WHERE [DateReceived] = CONVERT(date, DATEADD(dd,-@dayscounter,GETDATE()))
WHERE [DateReceived] = CONVERT(date, DATEADD(dd,-(@dayscounter+7),GETDATE()))

以下是可能对您有所帮助的不同查询。我假设数据存在于不同时间段的所有位置。

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]