我希望我能毫无错误地制定ddl + dml查询和我的问题。
我构建此查询
DECLARE @temp TEHOODOTRECHESH
(
VendorCode INT
, VendorName NVARCHAR(50)
, CheckDate DATETIME
, CheckSum DECIMAL(10,2)
, ObjType INT
)
INSERT INTO @temp TEHOODOTRECHESH (VendorCode, VendorName, CheckDate, CheckSum,ObjType
VALUES
(1, 'AAA', '20130101', 40,18),
(1, 'AAA', '20130101', 60,18),
(1, 'AAA', '20130101', 40,19),
(2, 'BBB', '20130303', 50,18),
(2, 'BBB', '20130601', 10,18),
(2, 'BBB', '20130604', 20,19)
SELECT * FROM
( SELECT
case when [ObjType]='18' then N'tr' else N'tz' end as 'DT',
CARDCODE,
CardName,
year(DocDueDate) as 'year',left(datename(month,DocDueDate),3) as [month],
DocTotal as 'Amount'
FROM TEHOODORRECHESH
WHERE DocStatus ='O' ) as monthsum
PIVOT
(
sum (Amount)
for [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct,
nov, dec) ) AS SUMPIVOT