本文发表在 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, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, [DEC]
FROM
(
SELECT
PRODUCT AS PRODUCT,
UPPER(CONVERT(CHAR(3), DATENAME(MONTH, SALES_DATE))) AS [MONTH],
SUM(SALES_AMOUNT) AS TOTAL_AMOUNT
FROM @SALES
GROUP BY PRODUCT, UPPER(CONVERT(CHAR(3), DATENAME(MONTH, SALES_DATE)))
) AS SRC
PIVOT (SUM(TOTAL_AMOUNT) FOR [MONTH] IN (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, [DEC])) AS RESULT
----- OUTPUT
PRODUCT JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
Tooth Brush 345.000 645.000 945.000 1245.000 1545.000 1845.000 2145.000 2445.000 2745.000 3045.000 3345.000 3645.000
Tooth Paste 333.000 633.000 933.000 1233.000 1533.000 1833.000 2133.000 2433.000 2733.000 3033.000 3333.000 3633.000更多精彩文章及讨论,请光临枫下论坛 rolia.net