SQL - 双透视 - 第二个透视(使用Count)无法按预期工作

时间:2014-12-27 08:31:51

标签: sql-server pivot dynamic-pivot

我有一张如下表格。我们称之为TBL1

                    SINIF                                        NAME_  CURCODE    TARIH       TARIHS     TAKSIT
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2015    2015 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2016    2016 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2016    2016 SAYI   28770,13
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD     2016    2016 SAYI   28732,89
     02 - YATIRIM KREDİSİ         TEB KREDİ - 100.000 EUR (ARAÇ KREDİ)      EUR     2015    2015 SAYI    2024,67
     02 - YATIRIM KREDİSİ         TEB KREDİ - 100.000 EUR (ARAÇ KREDİ)      EUR     2015    2015 SAYI    2024,67
     02 - YATIRIM KREDİSİ         TEB KREDİ - 100.000 EUR (ARAÇ KREDİ)      EUR     2015    2015 SAYI    2024,67

请注意,TARIHS只是TARIH的版本,最后添加了字符SAYI。我使用它是因为我需要双轴的不同列名。我想构建一个双轴,执行以下操作:

SUM(TAKSIT) vs TARIH (2015, 2016, 2017)
COUNT(TARIHS) vs TARIHS (2015 SAYI, 2016 SAYI, 2017 SAYI)

因此,我希望得到如下表格:

                    SINIF                                        NAME_  CURCODE        2015      2016    2017    2015 SAYI    2016 SAYI    2017 SAYI
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD    345243.6   86310.9    NULL           12            3         NULL
     02 - YATIRIM KREDİSİ         TEB KREDİ - 100.000 EUR (ARAÇ KREDİ)      EUR     6074.01      NULL    NULL            3         NULL         NULL

但是,当我构建双轴时,我得到以下内容:

                    SINIF                                        NAME_  CURCODE        2015      2016    2017    2015 SAYI    2016 SAYI    2017 SAYI
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD    345243.6      NULL    NULL            1            0            0
01 - TİCARİ (GENEL) KREDİ   VAKIFBANK KREDİ - 1.500.000 USD (TÜKETİCİ)      USD        NULL   86310.9    NULL            0            1            0
     02 - YATIRIM KREDİSİ         TEB KREDİ - 100.000 EUR (ARAÇ KREDİ)      EUR     6074.01      NULL    NULL            1            0            0

有人可以帮我处理我的代码吗?我没有在这里粘贴我的原始代码,因为它有点长,但我总结了它,让你得到一个大致的想法。

如果我从所有选项中删除TARIHS并且只想进行第一个数据透视,我还想告诉您我的代码是否有效。但是,当我将TARIHS的第二个轴添加到混合中时,我得到的表格不是我想要的。

谢谢!

2 个答案:

答案 0 :(得分:1)

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Test_Table(SINIF NVARCHAR(1000), NAME NVARCHAR(200)
, CURCODE NVARCHAR(1000) , TARIH INT, TARIHS VARCHAR(100), TAKSIT DECIMAL(10,2))

INSERT INTO Test_Table VALUES
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2015,'2015 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2016,'2016 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2016,'2016 SAYI',   28770.13),
(N'01 - TİCARİ (GENEL) KREDİ', N'VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ)','USD', 2016,'2016 SAYI',   28732.89),
  (N'02 - YATIRIM KREDİSİ',         N'TEB KREDİ - 100000 EUR (ARAÇ KREDİ)','EUR', 2015,'2015 SAYI',    2024.67),
  (N'02 - YATIRIM KREDİSİ',         N'TEB KREDİ - 100000 EUR (ARAÇ KREDİ)','EUR', 2015,'2015 SAYI',    2024.67),
  (N'02 - YATIRIM KREDİSİ',         N'TEB KREDİ - 100000 EUR (ARAÇ KREDİ)','EUR', 2015,'2015 SAYI',    2024.67)

查询1

SELECT * FROM 
(SELECT SINIF ,NAME ,CURCODE , CAST(TARIH AS NVARCHAR(10)) AS Years, TAKSIT  
 FROM Test_Table 
UNION ALL
SELECT SINIF , NAME , CURCODE, TARIHS , COUNT(*) counts FROM Test_Table
GROUP BY SINIF , NAME , CURCODE , TARIHS ) t 
  PIVOT (SUM(TAKSIT)
         FOR Years
         IN ([2015],[2016],[2017],[2015 SAYI],[2016 SAYI],[2017 SAYI])) P

<强> Results

|                     SINIF |                                     NAME | CURCODE |      2015 |     2016 |   2017 | 2015 SAYI | 2016 SAYI | 2017 SAYI |
|---------------------------|------------------------------------------|---------|-----------|----------|--------|-----------|-----------|-----------|
| 01 - TİCARİ (GENEL) KREDİ | VAKIFBANK KREDİ - 1500000 USD (TÜKETİCİ) |     USD | 345241.56 | 86273.15 | (null) |        12 |         3 |    (null) |
|      02 - YATIRIM KREDİSİ |      TEB KREDİ - 100000 EUR (ARAÇ KREDİ) |     EUR |   6074.01 |   (null) | (null) |         3 |    (null) |    (null) |

答案 1 :(得分:0)

问题可能是您在透视源查询中选择了所有列SINIF, NAME_ , CURCODE, TARIH, TARIHS, TAKSIT。尝试更改您的查询。

源数据透视查询中的

SELECT a.SINIF,a.NAME_,a.CURCODE,
       [2015],[2016],[2017],
       [2015 SAYI],[2016 SAYI],[2017 SAYI]
FROM  (SELECT *
       FROM   (SELECT SINIF,NAME_,CURCODE,TAKSIT,tarih
               FROM   Tablename) a
              PIVOT (Sum(TAKSIT)
                    FOR tarih IN([2015],[2016],[2017]))piv) a
      JOIN(SELECT *
           FROM   (SELECT SINIF,NAME_,CURCODE,TARIHS
                   FROM   Tablename) a
                  PIVOT (Count(TARIHS)
                        FOR TARIHS IN([2015 SAYI],[2016 SAYI],[2017 SAYI]))piv) b
        ON a.SINIF = b.SINIF
           AND a.NAME_ = b.NAME_
           AND a.CURCODE = b.CURCODE 

SQLFIDDLE DEMO