Store Grupa Quantity Value ITTYPE
1 A1 -44128 -101409.6 100
1 B1 -50000 -1500 101
1 E1 -32560 -53720.62 100
1 C1 -54730 -29006.9 101
1 D1 -45720 -37033.2 101
1 E1 -370500 -171100.61 100
2 E1 -370500 -227038.7 101
2 C1 -37152 -18575.94 100
2 C1 -145980 -52261.57 100
2 B1 -75600 -40639.65 100
2 B1 -69750 -64170 100

我想要Store和Grupa的分组
<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
我想要Store和Grupa的分组
ITTYPE 100 100 101 101
Store Grupa Quantity Value Quantity Value
1 A1 -44,128.00 -101,409.60 - -
1 B1 - - -50,000.00 -1,500.00
1 C1 - - -54,730.00 -29,006.90
1 D1 - - -45,720.00 -37,033.20
1 E1 -403,060.00 -224,821.23 - -
2 A1 - - - -
2 B1 -145,350.00 -104,809.65 - -
2 C1 -183,132.00 -70,837.51 - -
2 D1 - - - -
2 E1 - - -370,500.00 -227,038.70
&#13;
它像Store,Grupa和ITTYPE的总结
答案 0 :(得分:0)
您可以尝试这样的事情:
WITH RawData AS (
SELECT 1 AS Store, 'A1' AS Grupa, -44128 AS Quantity, -101409.6 AS [Value], 100 AS ITTYPE
UNION ALL
SELECT 1, 'B1', -50000, -1500, 101
UNION ALL
SELECT 1, 'E1', -32560, -53720.62, 100
UNION ALL
SELECT 1, 'C1', -54730, -29006.9, 101
UNION ALL
SELECT 1, 'D1', -45720, -37033.2, 101
UNION ALL
SELECT 1, 'E1', -370500, -171100.61, 100
UNION ALL
SELECT 2, 'E1', -370500, -227038.7, 101
UNION ALL
SELECT 2, 'C1', -37152, -18575.94, 100
UNION ALL
SELECT 2, 'C1', -145980, -52261.57, 100
UNION ALL
SELECT 2, 'B1', -75600, -40639.65, 100
UNION ALL
SELECT 2, 'B1', -69750, -64170, 100),
Store AS (
SELECT DISTINCT Store FROM RawData),
Grupa AS (
SELECT DISTINCT Grupa FROM RawData)
SELECT
s.Store,
g.Grupa,
SUM(CASE WHEN r.ITTYPE = 100 THEN r.Quantity END) AS [100_Quantity],
SUM(CASE WHEN r.ITTYPE = 100 THEN r.[Value] END) AS [100_Value],
SUM(CASE WHEN r.ITTYPE = 101 THEN r.Quantity END) AS [101_Quantity],
SUM(CASE WHEN r.ITTYPE = 101 THEN r.[Value] END) AS [101_Value]
FROM
Store s
CROSS JOIN Grupa g
LEFT JOIN RawData r ON r.Store = s.Store AND r.Grupa = g.Grupa
GROUP BY
s.Store,
g.Grupa
ORDER BY
1,2;
注意如果你有超过100和101的ITTYPES那么你可能会更好地使用动态PIVOT?
结果是:
Store Grupa 100_Quantity 100_Value 101_Quantity 101_Value
1 A1 -44128 -101409.60 NULL NULL
1 B1 NULL NULL -50000 -1500.00
1 C1 NULL NULL -54730 -29006.90
1 D1 NULL NULL -45720 -37033.20
1 E1 -403060 -224821.23 NULL NULL
2 A1 NULL NULL NULL NULL
2 B1 -145350 -104809.65 NULL NULL
2 C1 -183132 -70837.51 NULL NULL
2 D1 NULL NULL NULL NULL
2 E1 NULL NULL -370500 -227038.70
答案 1 :(得分:0)
试试这个:
DECLARE @table TABLE (
Store INT,Grupa NVARCHAR(5), Quantity DECIMAL(10,2), Value DECIMAL(10,2), ITTYPE INT)
INSERT INTO @table VALUES
(1 ,'A1', -44128 ,-101409.6 ,100),
(1 ,'B1', -50000 ,-1500 ,101),
(1 ,'E1', -32560 ,-53720.62 ,100),
(1 ,'C1', -54730 ,-29006.9 ,101),
(1 ,'D1', -45720 ,-37033.2 ,101),
(1 ,'E1', -370500 ,-171100.61 ,100),
(2 ,'E1', -370500 ,-227038.7 ,101),
(2 ,'C1', -37152 ,-18575.94 ,100),
(2 ,'C1', -145980 ,-52261.57 ,100),
(2 ,'B1', -75600 ,-40639.65 ,100),
(2 ,'B1', -69750 ,-64170 ,100)
SELECT Store ITTYPE,Grupa Grupa,SUM(CASE WHEN ITTYPE = 100 THEN Quantity ELSE '0' END) Quantity,
SUM(CASE WHEN ITTYPE = 100 THEN Value ELSE '0' END) Value,
SUM(CASE WHEN ITTYPE = 101 THEN Quantity ELSE '0' END) Quantity,
SUM(CASE WHEN ITTYPE = 101 THEN Value ELSE '0' END) Value
FROM @table GROUP BY Store,Grupa ORDER BY Store,Grupa
希望它有所帮助。 :)