尝试透视

时间:2016-09-03 17:20:18

标签: sql sql-server pivot

尝试在SQL Server中转移数据。得到错误,之前从未写过这样的东西。

现在看起来如何(我们每个类别都有数百个类别,请记住您的建议)

BLDGID  LEASID  SUITID   INCCAT  AMOUNT  PERIOD 
87000   100     AZ1        TAD    800   201601                                  
87000   200     AZ2        TAD    900   201603                                      
87000   300     AZ3        TAD    100   201607                                      
88000   400     AZ4        TAD    100   201607                                      
89000   500     AZ5        TAD    200   201609                                      
89000   600     AZ6        TAD    900   201611                                      
90000   700     AZ7        TAD    500   201612                                      
90000   800     AZ8        TAD    950   201602                                      

我希望它看起来如何

BLDGID  LEASID  SUITID  INCCAT  Jan Feb Mar Apr May June    Jul Aug Sept    Oct Nov Dec
87000   100      AZ1    TAD     800                                         
87000   200      AZ2    TAD             900                                 
87000   300      AZ3    TAD                         1000                    
88000   400      AZ4    TAD                                 100                 
89000   500      AZ5    TAD                                          200            
89000   600      AZ6    TAD                                                     900 
90000   700      AZ7    TAD                                                         500
90000   800      AZ8    TAD         950                                     

我一直在尝试的代码(不确定我是否在正确的轨道上)

SELECT  c.BLDGID AS 'BLDGID', 
        c.LEASID AS 'LEASID', 
        l.OCCPNAME AS 'OCCPNAME', 
        l.SUITID AS 'SUITID', 
        c.INCCAT AS 'INCCAT', 
        c.SRCCODE AS 'SRCCODE', 
        c.TRANAMT AS 'TRANAMT', 
        c.DESCRPTN AS 'DESCRPTN', 
        c.PERIOD AS 'PERIOD'
FROM SQLDATA.dbo.LEAS l
INNER JOIN SQLDATA.dbo.CMLEDG c
    ON l.BLDGID = c.BLDGID AND SQLDATA.dbo.LEAS.LEASID = c.LEASID
PIVOT (
    MAX(TRANAMT) 
    FOR PERIOD IN ([201601],[201602],[201603],[201607],[201609],[201611],[201612])
) as REVENUEPERITEM
WHERE  c.BLDGID &SPARM01
    AND c.INCCAT &SPARM02 
    AND c.SRCCODE NOT LIKE 'CR'
    AND c.DESCRPTN NOT LIKE 'CREDITAPPLY' 
    AND c.DESCRPTN NOT LIKE 'RECEIPT'
    AND c.PERIOD > '201512'

在底部查看编辑

作为旁注,我们在工作中使用特殊程序 SPARM 您看到的内容是该程序的项目允许我编辑查询而无需重新编写它。

我遇到的问题是错误,

  

SQL语句中存在错误,Pivot分组列必须具有可比性。列的类型" ADDLDESC"是" text",这是无法比较的。

新编辑开始

我目前的结果来自GOFR的解决方案。

BLDGID  LEASID  SUITID  INCCAT  Jan Feb Mar Apr May June    Jul Aug Sept    Oct Nov Dec
87000   100      AZ1    TAD     800                                         
87000   100      AZ1    TAD         800
87000   200      AZ2    TAD             900                                 
87000   300      AZ3    TAD                         1000                    
88000   400      AZ4    TAD                                 100                 
89000   500      AZ5    TAD                                          200            
89000   600      AZ6    TAD                                                     900 
90000   700      AZ7    TAD                                                         500
90000   800      AZ8    TAD         950                                     

看看第1行和第2行是否有相同的西装,我想将它们组合成一行。整个表格都有每个套装的副本,所以看起来如下所示。

BLDGID  LEASID  SUITID  INCCAT  Jan Feb Mar Apr May June    Jul Aug Sept    Oct Nov Dec
87000   100      AZ1    TAD     800 800                                     
87000   200      AZ2    TAD             900                                 
87000   300      AZ3    TAD                         1000                    
88000   400      AZ4    TAD                                 100                 
89000   500      AZ5    TAD                                          200            
89000   600      AZ6    TAD                                                     900 
90000   700      AZ7    TAD                                                         500
90000   800      AZ8    TAD         950                 

1 个答案:

答案 0 :(得分:3)

您的查询几乎就在那里,尝试像这样重建它:

SELECT *
FROM (
    SELECT  c.BLDGID AS 'BLDGID', 
            c.LEASID AS 'LEASID', 
            l.OCCPNAME AS 'OCCPNAME', 
            l.SUITID AS 'SUITID', 
            c.INCCAT AS 'INCCAT', 
            c.SRCCODE AS 'SRCCODE', 
            c.TRANAMT AS 'TRANAMT', 
            c.DESCRPTN AS 'DESCRPTN', 
            c.PERIOD AS 'PERIOD'
    FROM SQLDATA.dbo.LEAS l
    INNER JOIN SQLDATA.dbo.CMLEDG c
        ON l.BLDGID = c.BLDGID AND l.LEASID = c.LEASID
    WHERE  c.BLDGID &SPARM01
        AND c.INCCAT &SPARM02 
        AND c.SRCCODE NOT LIKE 'CR'
        AND c.DESCRPTN NOT LIKE 'CREDITAPPLY' 
        AND c.DESCRPTN NOT LIKE 'RECEIPT'
        AND c.PERIOD > '201512'
    ) as t
PIVOT (
    MAX(TRANAMT) 
    FOR PERIOD IN ([201601],[201602],[201603],[201607],[201609],[201611],[201612])
) as REVENUEPERITEM

以下是你可以如何转动所拥有的东西(我使用CTE,因为我没有你的桌子):

;WITH cte AS (
SELECT *
FROM (VALUES
(87000,   100,     'AZ1',        'TAD',    800,   '201601'),
(87000,   200,     'AZ2',        'TAD',    900,   '201603'),
(87000,   300,     'AZ3',        'TAD',    100,   '201607'),
(88000,   400,     'AZ4',        'TAD',    100,   '201607'),                                     
(89000,   500,     'AZ5',        'TAD',    200,   '201609'),
(89000,   600,     'AZ6',        'TAD',    900,   '201611'),
(90000,   700,     'AZ7',        'TAD',    500,   '201612'),
(90000,   800,     'AZ8',        'TAD',    950,   '201602')
) as t([BLDGID], [LEASID], [SUITID], [INCCAT], [AMOUNT], [PERIOD])
)

SELECT *
FROM cte
PIVOT (
    MAX([AMOUNT]) FOR [PERIOD] IN ([201601],[201602],[201603],[201604])
) pvt

输出:

BLDGID  LEASID  SUITID  INCCAT  201601  201602  201603  201604
87000   100     AZ1     TAD     800     NULL    NULL    NULL
87000   200     AZ2     TAD     NULL    NULL    900     NULL
87000   300     AZ3     TAD     NULL    NULL    NULL    NULL
88000   400     AZ4     TAD     NULL    NULL    NULL    NULL
89000   500     AZ5     TAD     NULL    NULL    NULL    NULL
89000   600     AZ6     TAD     NULL    NULL    NULL    NULL
90000   700     AZ7     TAD     NULL    NULL    NULL    NULL
90000   800     AZ8     TAD     NULL    950     NULL    NULL