我认为一切正常 -
询问:
DECLARE @temp TABLE
(
VendorCode INT
, VendorName NVARCHAR(50)
, CheckDate DATETIME
, Canceled CHAR(1)
, [CheckSum] DECIMAL(10,2)
)
INSERT INTO @temp (VendorCode, VendorName, CheckDate, Canceled, [CheckSum])
VALUES
(1, '1', '20100101', 'N', 4),
(2, '2', '20100303', 'N', 2),
(2, '2', '20100601', 'N', 1),
(2, '2', '20100604', 'N', 99)
SELECT *
FROM
(
SELECT
[VendorCode]
, [VendorName]
, [year] = YEAR(CheckDate)
, [month] = LEFT(DATENAME(MONTH, CheckDate), 3)
, Amount = [CheckSum]
FROM @temp
WHERE Canceled = 'N'
) abc
PIVOT
(
SUM(Amount)
FOR [month] IN (
jan, feb, mar,
apr, may, jun,
jul, aug, sep,
oct, nov, [dec])
) sumpivot
输出:
VendorCode VendorName year jan feb mar apr may jun jul aug sep oct nov dec
----------- ---------- ------ ------ ----- ----- ----- ----- ------- ----- ----- ----- ------ ------ ------
1 1 2010 4.00 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
2 2 2010 NULL NULL 2.00 NULL NULL 100.00 NULL NULL NULL NULL NULL NULL