本文发表在 rolia.net 枫下论坛DECLARE @Sales TABLE
(
[PRODUCT] VARCHAR(20),
[SALES_DATE] DATETIME,
[SALES_AMOUNT] NUMERIC(10,3)
)
INSERT INTO @Sales
VALUES
('Tooth Paste', '2014-01-01', 101.00),
('Tooth Paste', '2014-01-11', 111.00),
('Tooth Paste', '2014-01-21', 121.00),
('Tooth Paste', '2014-02-01', 201.00),
('Tooth Paste', '2014-02-11', 211.00),
('Tooth Paste', '2014-02-21', 221.00),
('Tooth Paste', '2014-03-01', 301.00),
('Tooth Paste', '2014-03-11', 311.00),
('Tooth Paste', '2014-03-21', 321.00),
('Tooth Paste', '2014-04-01', 401.00),
('Tooth Paste', '2014-04-11', 411.00),
('Tooth Paste', '2014-04-21', 421.00),
('Tooth Paste', '2014-05-01', 501.00),
('Tooth Paste', '2014-05-11', 511.00),
('Tooth Paste', '2014-05-21', 521.00),
('Tooth Paste', '2014-06-01', 601.00),
('Tooth Paste', '2014-06-11', 611.00),
('Tooth Paste', '2014-06-21', 621.00),
('Tooth Paste', '2014-07-01', 701.00),
('Tooth Paste', '2014-07-11', 711.00),
('Tooth Paste', '2014-07-21', 721.00),
('Tooth Paste', '2014-08-01', 801.00),
('Tooth Paste', '2014-08-11', 811.00),
('Tooth Paste', '2014-08-21', 821.00),
('Tooth Paste', '2014-09-01', 901.00),
('Tooth Paste', '2014-09-11', 911.00),
('Tooth Paste', '2014-09-21', 921.00),
('Tooth Paste', '2014-10-01', 1001.00),
('Tooth Paste', '2014-10-11', 1011.00),
('Tooth Paste', '2014-10-21', 1021.00),
('Tooth Paste', '2014-11-01', 1101.00),
('Tooth Paste', '2014-11-11', 1111.00),
('Tooth Paste', '2014-11-21', 1121.00),
('Tooth Paste', '2014-12-01', 1201.00),
('Tooth Paste', '2014-12-11', 1211.00),
('Tooth Paste', '2014-12-21', 1221.00),
('Tooth Brush', '2014-01-05', 105.00),
('Tooth Brush', '2014-01-15', 115.00),
('Tooth Brush', '2014-01-25', 125.00),
('Tooth Brush', '2014-02-05', 205.00),
('Tooth Brush', '2014-02-15', 215.00),
('Tooth Brush', '2014-02-25', 225.00),
('Tooth Brush', '2014-03-05', 305.00),
('Tooth Brush', '2014-03-15', 315.00),
('Tooth Brush', '2014-03-25', 325.00),
('Tooth Brush', '2014-04-05', 405.00),
('Tooth Brush', '2014-04-15', 415.00),
('Tooth Brush', '2014-04-25', 425.00),
('Tooth Brush', '2014-05-05', 505.00),
('Tooth Brush', '2014-05-15', 515.00),
('Tooth Brush', '2014-05-25', 525.00),
('Tooth Brush', '2014-06-05', 605.00),
('Tooth Brush', '2014-06-15', 615.00),
('Tooth Brush', '2014-06-25', 625.00),
('Tooth Brush', '2014-07-05', 705.00),
('Tooth Brush', '2014-07-15', 715.00),
('Tooth Brush', '2014-07-25', 725.00),
('Tooth Brush', '2014-08-05', 805.00),
('Tooth Brush', '2014-08-15', 815.00),
('Tooth Brush', '2014-08-25', 825.00),
('Tooth Brush', '2014-09-05', 905.00),
('Tooth Brush', '2014-09-15', 915.00),
('Tooth Brush', '2014-09-25', 925.00),
('Tooth Brush', '2014-10-05', 1005.00),
('Tooth Brush', '2014-10-15', 1015.00),
('Tooth Brush', '2014-10-25', 1025.00),
('Tooth Brush', '2014-11-05', 1105.00),
('Tooth Brush', '2014-11-15', 1115.00),
('Tooth Brush', '2014-11-25', 1125.00),
('Tooth Brush', '2014-12-05', 1205.00),
('Tooth Brush', '2014-12-15', 1215.00),
('Tooth Brush', '2014-12-25', 1225.00);
select PRODUCT,SUM(JAN) AS JAN,SUM(FEB) AS FEB,SUM(MAR) AS MAR,SUM(APR) AS APR,SUM(MAY) AS MAY,SUM(JUN) AS JUN,SUM(JUL) AS JUL,SUM(AUG) AS AUG,SUM(SEP) AS SEP,SUM(OCT) AS OCT,SUM(NOV) AS NOV,SUM([DEC]) AS [DEC]
from(
select product,
MAX(Case month([SALES_DATE]) When 1 Then [SALES_AMOUNT] End) JAN,
MAX(Case month([SALES_DATE]) When 2 Then [SALES_AMOUNT] end) FEB,
MAX(Case month([SALES_DATE]) When 3 Then [SALES_AMOUNT] end) MAR,
MAX(Case month([SALES_DATE]) When 4 Then [SALES_AMOUNT] end) APR,
MAX(Case month([SALES_DATE]) When 5 Then [SALES_AMOUNT] end) MAY,
MAX(Case month([SALES_DATE]) When 6 Then [SALES_AMOUNT] end) JUN,
MAX(Case month([SALES_DATE]) When 7 Then [SALES_AMOUNT] end) JUL,
MAX(Case month([SALES_DATE]) When 8 Then [SALES_AMOUNT] end) AUG,
MAX(Case month([SALES_DATE]) When 9 Then [SALES_AMOUNT] end) SEP,
MAX(Case month([SALES_DATE]) When 10 Then [SALES_AMOUNT] end) OCT,
MAX(Case month([SALES_DATE]) When 11 Then [SALES_AMOUNT] end) NOV,
MAX(Case month([SALES_DATE]) When 12 Then [SALES_AMOUNT] end) [DEC]
from @SALES
group by Product,[SALES_AMOUNT]
) as List
group by product更多精彩文章及讨论,请光临枫下论坛 rolia.net
(
[PRODUCT] VARCHAR(20),
[SALES_DATE] DATETIME,
[SALES_AMOUNT] NUMERIC(10,3)
)
INSERT INTO @Sales
VALUES
('Tooth Paste', '2014-01-01', 101.00),
('Tooth Paste', '2014-01-11', 111.00),
('Tooth Paste', '2014-01-21', 121.00),
('Tooth Paste', '2014-02-01', 201.00),
('Tooth Paste', '2014-02-11', 211.00),
('Tooth Paste', '2014-02-21', 221.00),
('Tooth Paste', '2014-03-01', 301.00),
('Tooth Paste', '2014-03-11', 311.00),
('Tooth Paste', '2014-03-21', 321.00),
('Tooth Paste', '2014-04-01', 401.00),
('Tooth Paste', '2014-04-11', 411.00),
('Tooth Paste', '2014-04-21', 421.00),
('Tooth Paste', '2014-05-01', 501.00),
('Tooth Paste', '2014-05-11', 511.00),
('Tooth Paste', '2014-05-21', 521.00),
('Tooth Paste', '2014-06-01', 601.00),
('Tooth Paste', '2014-06-11', 611.00),
('Tooth Paste', '2014-06-21', 621.00),
('Tooth Paste', '2014-07-01', 701.00),
('Tooth Paste', '2014-07-11', 711.00),
('Tooth Paste', '2014-07-21', 721.00),
('Tooth Paste', '2014-08-01', 801.00),
('Tooth Paste', '2014-08-11', 811.00),
('Tooth Paste', '2014-08-21', 821.00),
('Tooth Paste', '2014-09-01', 901.00),
('Tooth Paste', '2014-09-11', 911.00),
('Tooth Paste', '2014-09-21', 921.00),
('Tooth Paste', '2014-10-01', 1001.00),
('Tooth Paste', '2014-10-11', 1011.00),
('Tooth Paste', '2014-10-21', 1021.00),
('Tooth Paste', '2014-11-01', 1101.00),
('Tooth Paste', '2014-11-11', 1111.00),
('Tooth Paste', '2014-11-21', 1121.00),
('Tooth Paste', '2014-12-01', 1201.00),
('Tooth Paste', '2014-12-11', 1211.00),
('Tooth Paste', '2014-12-21', 1221.00),
('Tooth Brush', '2014-01-05', 105.00),
('Tooth Brush', '2014-01-15', 115.00),
('Tooth Brush', '2014-01-25', 125.00),
('Tooth Brush', '2014-02-05', 205.00),
('Tooth Brush', '2014-02-15', 215.00),
('Tooth Brush', '2014-02-25', 225.00),
('Tooth Brush', '2014-03-05', 305.00),
('Tooth Brush', '2014-03-15', 315.00),
('Tooth Brush', '2014-03-25', 325.00),
('Tooth Brush', '2014-04-05', 405.00),
('Tooth Brush', '2014-04-15', 415.00),
('Tooth Brush', '2014-04-25', 425.00),
('Tooth Brush', '2014-05-05', 505.00),
('Tooth Brush', '2014-05-15', 515.00),
('Tooth Brush', '2014-05-25', 525.00),
('Tooth Brush', '2014-06-05', 605.00),
('Tooth Brush', '2014-06-15', 615.00),
('Tooth Brush', '2014-06-25', 625.00),
('Tooth Brush', '2014-07-05', 705.00),
('Tooth Brush', '2014-07-15', 715.00),
('Tooth Brush', '2014-07-25', 725.00),
('Tooth Brush', '2014-08-05', 805.00),
('Tooth Brush', '2014-08-15', 815.00),
('Tooth Brush', '2014-08-25', 825.00),
('Tooth Brush', '2014-09-05', 905.00),
('Tooth Brush', '2014-09-15', 915.00),
('Tooth Brush', '2014-09-25', 925.00),
('Tooth Brush', '2014-10-05', 1005.00),
('Tooth Brush', '2014-10-15', 1015.00),
('Tooth Brush', '2014-10-25', 1025.00),
('Tooth Brush', '2014-11-05', 1105.00),
('Tooth Brush', '2014-11-15', 1115.00),
('Tooth Brush', '2014-11-25', 1125.00),
('Tooth Brush', '2014-12-05', 1205.00),
('Tooth Brush', '2014-12-15', 1215.00),
('Tooth Brush', '2014-12-25', 1225.00);
select PRODUCT,SUM(JAN) AS JAN,SUM(FEB) AS FEB,SUM(MAR) AS MAR,SUM(APR) AS APR,SUM(MAY) AS MAY,SUM(JUN) AS JUN,SUM(JUL) AS JUL,SUM(AUG) AS AUG,SUM(SEP) AS SEP,SUM(OCT) AS OCT,SUM(NOV) AS NOV,SUM([DEC]) AS [DEC]
from(
select product,
MAX(Case month([SALES_DATE]) When 1 Then [SALES_AMOUNT] End) JAN,
MAX(Case month([SALES_DATE]) When 2 Then [SALES_AMOUNT] end) FEB,
MAX(Case month([SALES_DATE]) When 3 Then [SALES_AMOUNT] end) MAR,
MAX(Case month([SALES_DATE]) When 4 Then [SALES_AMOUNT] end) APR,
MAX(Case month([SALES_DATE]) When 5 Then [SALES_AMOUNT] end) MAY,
MAX(Case month([SALES_DATE]) When 6 Then [SALES_AMOUNT] end) JUN,
MAX(Case month([SALES_DATE]) When 7 Then [SALES_AMOUNT] end) JUL,
MAX(Case month([SALES_DATE]) When 8 Then [SALES_AMOUNT] end) AUG,
MAX(Case month([SALES_DATE]) When 9 Then [SALES_AMOUNT] end) SEP,
MAX(Case month([SALES_DATE]) When 10 Then [SALES_AMOUNT] end) OCT,
MAX(Case month([SALES_DATE]) When 11 Then [SALES_AMOUNT] end) NOV,
MAX(Case month([SALES_DATE]) When 12 Then [SALES_AMOUNT] end) [DEC]
from @SALES
group by Product,[SALES_AMOUNT]
) as List
group by product更多精彩文章及讨论,请光临枫下论坛 rolia.net