PIVOT和总和两列

时间:2013-08-08 08:21:53

标签: pivot

我希望我能毫无错误地制定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

  1. 我想总结来自ObjType 18/19(仅两种类型)的文档行,并将objtype与月份相加。
  2. 是否有一个机会,如果ObjType 19把数字减去opertaor( - ) 或者它将在Brackets()
  3. 当我通过仅供应商代码编写订单时,它可以工作,但如果我通过以下方式添加订单:vendorcode + objtype 它不起作用。为什么呢?

0 个答案:

没有答案