SQL - 将静态数据透视转换为动态数据透视表

时间:2014-12-22 18:30:06

标签: sql-server dynamic pivot pivot-table dynamic-pivot

我有以下静态支点:

SELECT * 
FROM

(

SELECT

 d.CODE,
 a.ANAPARA - Isnull(b.ANAPARA,0) AS ANAPARA,
 d.NAME_,
 c.DUEDATE,
 a.TAKSIT - Isnull(b.TAKSIT,0) AS TAKSIT,
 e.CURCODE



FROM
    (SELECT 
 PARENTREF,
 SUM(TOTAL) AS ANAPARA,
 SUM(INTTOTAL) AS FAIZ,
 SUM(BSMVTOTAL) AS BSMV,
 SUM(KKDFTOTAL) AS KKDF,
 SUM(TOTAL+INTTOTAL+BSMVTOTAL+KKDFTOTAL) AS TAKSIT

        FROM LG_011_BNCREPAYTR 
        WHERE TRANSTYPE = 0 
        GROUP BY PARENTREF) a 

LEFT OUTER JOIN (SELECT
 PARENTREF,
 SUM(TOTAL) AS ANAPARA,
 SUM(INTTOTAL) AS FAIZ,
 SUM(BSMVTOTAL) AS BSMV,
 SUM(KKDFTOTAL) AS KKDF,
 SUM(TOTAL+INTTOTAL+BSMVTOTAL+KKDFTOTAL) AS TAKSIT
        FROM LG_011_BNCREPAYTR 
        WHERE TRANSTYPE = 1 
        GROUP BY PARENTREF) b 
ON a.PARENTREF = b.PARENTREF

INNER JOIN (SELECT
 PARENTREF,
 CREDITREF,
 DUEDATE,
 OPRDATE
 FROM LG_011_BNCREPAYTR) c
ON a.PARENTREF=c.PARENTREF

INNER JOIN LG_011_BNCREDITCARD d
ON c.CREDITREF=d.LOGICALREF

INNER JOIN L_CURRENCYLIST e
ON d.TRCURR=e.CURTYPE OR (d.TRCURR=0 AND e.CURTYPE=160)

WHERE e.FIRMNR=11

) x
PIVOT
(
SUM(TAKSIT)
FOR CURCODE IN ([USD],[EUR],[YEN])

) xx

创建下表:

CODE      ANAPARA             NAME_          DUEDATE          EUR        USD        YEN
 001       103.35          CAR LOAN       2015-01-01         NULL     150.01       NULL   
 002       106.89     CONSUMER LOAN       2015-03-10       190.35       NULL       NULL
 003       110.44          CAR LOAN       2015-04-20         NULL       NULL     200.51
 004       103.22          CAR LOAN       2015-05-04       150.02       NULL       NULL

但是,我想将其转换为动态数据透视表。因此,我想更改代码,以便从e.CURCODE获取不同的货币代码并生成相同的数据透视表。

感谢很多。

0 个答案:

没有答案