2016-05-24 68 views
1

的底部,我想創建以下文件:和數樣本的接收,樣品的總和接受8天前,區別兩個總和與總之間的

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 

從表OrderInfo的

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

與表LocationInfo

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年5月23日」與「2016年5月16日」,但無法弄清楚如何加入兩個查詢,獲得兩個查詢之間的差異,並將結果與​​底部的總數進行排序。

我還希望以相同的格式創建結果,但將星期一和上星期一替換爲星期至今和上一星期至今以及月至今和上個月至今的一個。

任何幫助將不勝感激。

+0

where DateReceived ='2016-05-23' 你想要的數據只是爲那天或整個星期,即從16日到23日? –

回答

0

以下是可能對您有幫助的不同查詢。我假設數據存在於不同時間段的所有地點。

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] 
+0

如果第一個星期一有記錄的位置,但第二個星期一沒有記錄,則不會顯示。然而總計將包括它。 0(或缺失)是可能的計數 –

0

假設你的意思是你想的計數全周通往星期一:

--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,[email protected],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,[email protected],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,[email protected],GETDATE())) 
WHERE [DateReceived] = CONVERT(date, DATEADD(dd,-(@dayscounter+7),GETDATE()))