我有以下查询;
SELECT STOCK_CODE,
dbo.manu_STOCK.DESCRIPTION,
QTY_IN_STOCK,
Quantity,
ForecastDate
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
这会显示以下信息;
STOCK_CODE DESCRIPTION QTY_INSTOCK Quantity ForecastDate
523 gel 12 10 01/08/2014
523 gel 12 10 08/08/2014
我希望能够修改查询,以便显示以下格式
Stock Code Description WK1 WK2
523 gel 22 22
因此它将在第一个日期对库存和数量进行总和,该列将被称为wk1,第二周 - wk2等。
你能就此提出建议吗?
答案 0 :(得分:1)
如果要将结果转动52周,可以使用以下查询。这将使数据转向52周。这只是概念,我无法测试此查询。
SELECT * FROM
( SELECT
dbo.manu_STOCK.STOCK_CODE AS [Stock Code],
dbo.manu_STOCK.DESCRIPTION,
DATEPART(WEEK,[Date]) Wk,
QTY_IN_STOCK + Quantity AS Stock
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
) AS Source
PIVOT
(
SUM(Stock)
FOR WK IN
([0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
,[11],[12],[13],[14],[15],[16],[17],[18],[19],[20]
,[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]
,[31],[32],[33],[34],[35],[36],[37],[38],[39],[40]
,[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]
,[51],[52])
) AS PVT ;
答案 1 :(得分:0)
DECLARE @Stock TABLE
(
STOCK_CODE INT,
DESCRIPTION VARCHAR(50),
QTY_INSTOCK INT,
QUANTITY INT,
FORECASTDATE DATETIME
)
INSERT INTO @Stock
( STOCK_CODE, DESCRIPTION, QTY_INSTOCK, QUANTITY, FORECASTDATE )
VALUES
( 523, 'gel', 12, 10, '01/08/2014' ),
( 523, 'gel', 12, 10, '08/08/2014' )
SELECT
s.STOCK_CODE,
s.DESCRIPTION,
s.STOCKYEAR,
SUM(CASE WHEN s.STOCKWEEK = 1 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk1,
SUM(CASE WHEN s.STOCKWEEK = 2 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk2,
SUM(CASE WHEN s.STOCKWEEK = 3 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk3,
SUM(CASE WHEN s.STOCKWEEK = 4 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk4,
SUM(CASE WHEN s.STOCKWEEK = 5 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk5,
SUM(CASE WHEN s.STOCKWEEK = 6 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk6,
SUM(CASE WHEN s.STOCKWEEK = 7 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk7,
SUM(CASE WHEN s.STOCKWEEK = 8 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk8,
SUM(CASE WHEN s.STOCKWEEK = 9 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk9,
SUM(CASE WHEN s.STOCKWEEK = 10 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk10,
SUM(CASE WHEN s.STOCKWEEK = 11 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk11,
SUM(CASE WHEN s.STOCKWEEK = 12 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk12,
SUM(CASE WHEN s.STOCKWEEK = 13 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk13,
SUM(CASE WHEN s.STOCKWEEK = 14 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk14,
SUM(CASE WHEN s.STOCKWEEK = 15 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk15,
SUM(CASE WHEN s.STOCKWEEK = 16 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk16,
SUM(CASE WHEN s.STOCKWEEK = 17 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk17,
SUM(CASE WHEN s.STOCKWEEK = 18 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk18,
SUM(CASE WHEN s.STOCKWEEK = 19 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk19,
SUM(CASE WHEN s.STOCKWEEK = 20 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk20,
SUM(CASE WHEN s.STOCKWEEK = 21 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk21,
SUM(CASE WHEN s.STOCKWEEK = 22 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk22,
SUM(CASE WHEN s.STOCKWEEK = 23 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk23,
SUM(CASE WHEN s.STOCKWEEK = 24 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk24,
SUM(CASE WHEN s.STOCKWEEK = 25 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk25,
SUM(CASE WHEN s.STOCKWEEK = 26 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk26,
SUM(CASE WHEN s.STOCKWEEK = 27 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk27,
SUM(CASE WHEN s.STOCKWEEK = 28 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk28,
SUM(CASE WHEN s.STOCKWEEK = 29 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk29,
SUM(CASE WHEN s.STOCKWEEK = 30 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk30,
SUM(CASE WHEN s.STOCKWEEK = 31 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk31,
SUM(CASE WHEN s.STOCKWEEK = 32 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk32,
SUM(CASE WHEN s.STOCKWEEK = 33 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk33,
SUM(CASE WHEN s.STOCKWEEK = 34 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk34,
SUM(CASE WHEN s.STOCKWEEK = 35 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk35,
SUM(CASE WHEN s.STOCKWEEK = 36 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk36,
SUM(CASE WHEN s.STOCKWEEK = 37 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk37,
SUM(CASE WHEN s.STOCKWEEK = 38 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk38,
SUM(CASE WHEN s.STOCKWEEK = 39 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk39,
SUM(CASE WHEN s.STOCKWEEK = 40 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk40,
SUM(CASE WHEN s.STOCKWEEK = 41 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk41,
SUM(CASE WHEN s.STOCKWEEK = 42 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk42,
SUM(CASE WHEN s.STOCKWEEK = 43 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk43,
SUM(CASE WHEN s.STOCKWEEK = 44 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk44,
SUM(CASE WHEN s.STOCKWEEK = 45 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk45,
SUM(CASE WHEN s.STOCKWEEK = 46 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk46,
SUM(CASE WHEN s.STOCKWEEK = 47 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk47,
SUM(CASE WHEN s.STOCKWEEK = 48 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk48,
SUM(CASE WHEN s.STOCKWEEK = 49 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk49,
SUM(CASE WHEN s.STOCKWEEK = 50 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk50,
SUM(CASE WHEN s.STOCKWEEK = 51 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk51,
SUM(CASE WHEN s.STOCKWEEK = 52 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk52,
SUM(CASE WHEN s.STOCKWEEK = 53 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk53
FROM
(
SELECT s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE) STOCKYEAR, DATEPART(WEEK, s.FORECASTDATE) STOCKWEEK, SUM(s.QTY_INSTOCK) QTY_INSTOCK, SUM(s.QUANTITY) QTY
FROM @Stock s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE), DATEPART(WEEK, s.FORECASTDATE)
) s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
ORDER BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
按代码,desc,年份和星期分组,然后按一周的总和计算一年中的每周(根据每年的开始/结束而增加额外的一周。