具有多列的Unpivot查询

时间:2017-04-17 09:04:30

标签: sql oracle unpivot

我有下面的表格(EMPLOYEE_NUMBER Next Academic Rank Max Academic Rank Max Score Academic Rank Promotion Rank ACADEMIC_RANK_START_DATE ACADEMIC_RANK_END_DATE Income_code Date(YYYY) Date(MON) Date(DD) Amount Tax? INCOME_START_DATE INCOME_END_DATE --------------- ------------------ ------------------ ---------- ------------- -------------- ------------------------ ------------------ ----------- --------- --------- --------- ------ ----- ----------------- ------------------- 33333 Professor Y 10.1 Ass.Professor Y 01-JAN-2017 31-DEC-4712 P 2017 JAN 01 10000 Tax 01-JAN-2017 31-DEC-4712 44444 Ass.Professor N 9.7 Student N 01-JAN-2017 31-DEC-4712 R 2017 JAN 17 50000 Non-Tax 01-JAN-2017 31-DEC-4712 ):

EMPLOYEE_NUMBER     EXTRA_INFORMATION       INFORMATION_CATEGORY    INFORMATION1    INFORMATION2    INFORMATION3
------------------- ---------------------   ----------------------  --------------  -------------   ------------
33333               Academic Rank           Next Academic Rank      Professor       Y               10.1        
33333               Academic Rank           Academic Rank           Ass.Professor   N               Y           
33333               Academic Rank           Effective Start Date    01-JAN-2017     (NULL)          (NULL)      
33333               Academic Rank           Effective End Date      31-DEC-4712     (NULL)          (NULL)      
33333               Alien Income Forecast   Income_code             P               (NULL)          (NULL)      
33333               Alien Income Forecast   Date                    2017            01              JAN         
33333               Alien Income Forecast   Amount                  10000           Tax             (NULL)      
33333               Alien Income Forecast   Effective Start Date    01-JAN-2017     (NULL)          (NULL)      
33333               Alien Income Forecast   Effective End Date      31-DEC-4712     (NULL)          (NULL)      
44444               Academic Rank           Next Academic Rank      Ass.Professor   N               9.7         
44444               Academic Rank           Academic Rank           Student         N               (NULL)      
44444               Academic Rank           Effective Start Date    01-JAN-2017     (NULL)          (NULL)      
44444               Academic Rank           Effective End Date      31-DEC-4712     (NULL)          (NULL)      
44444               Alien Income Forecast   Income_code             R               (NULL)          (NULL)      
44444               Alien Income Forecast   Date                    2017            17              JAN         
44444               Alien Income Forecast   Amount                  50000           Non-Tax         (NULL)      
44444               Alien Income Forecast   Effective Start Date    01-JAN-2017     (NULL)          (NULL)      
44444               Alien Income Forecast   Effective End Date      31-DEC-4712     (NULL)          (NULL)      

我如何取消透视使其在方向上垂直,如下所示?

CREATE TABLE EMP_EIT_EXT    
(
    EMPLOYEE_NUMBER          VARCHAR2(250),
    Next_Academic_Rank       VARCHAR2(250),
    Max_Academic_Rank        VARCHAR2(250),
    Max_Score                VARCHAR2(250),
    Academic_Rank            VARCHAR2(250),
    Promotion_Rank           VARCHAR2(250),
    ACADEMIC_RANK_START_DATE VARCHAR2(250),
    ACADEMIC_RANK_END_DATE   VARCHAR2(250),
    Income_code              VARCHAR2(250),
    Date_YYYY                VARCHAR2(250),
    Date_MON                 VARCHAR2(250),
    Date_DD                  VARCHAR2(250),
    Amount                   VARCHAR2(250),
    Tax                      VARCHAR2(250),
    INCOME_START_DATE        VARCHAR2(250),
    INCOME_END_DATE          VARCHAR2(250)
);

INSERT INTO EMP_EIT_EXT (EMPLOYEE_NUMBER,Next_Academic_Rank,Max_Academic_Rank,Max_Score,Academic_Rank,Promotion_Rank,ACADEMIC_RANK_START_DATE ,ACADEMIC_RANK_END_DATE,Income_code,Date_YYYY,Date_MON,Date_DD,Amount,Tax,INCOME_START_DATE,INCOME_END_DATE)
VALUES('33333','Professor','Y','10.1','Ass.Professor','Y','01-JAN-2017','31-DEC-4712','P','2017','JAN','01','10000','Tax','01-JAN-2017','31-DEC-4712');
INSERT INTO EMP_EIT_EXT (EMPLOYEE_NUMBER,Next_Academic_Rank,Max_Academic_Rank,Max_Score,Academic_Rank,Promotion_Rank,ACADEMIC_RANK_START_DATE ,ACADEMIC_RANK_END_DATE,Income_code,Date_YYYY,Date_MON,Date_DD,Amount,Tax,INCOME_START_DATE,INCOME_END_DATE)
VALUES('44444','Ass.Professor','N','9.7','Student','N','01-JAN-2017','31-DEC-4712','R','2017','JAN','17','50000','Non-Tax','01-JAN-2017','31-DEC-4712');

样本DDL和DML

UNION

我知道这可以通过UNION ALL或{{1}}实现,但我想尽可能使用UnPivot。

1 个答案:

答案 0 :(得分:-1)

不确定。只需填写TraMineR个语句,然后将其与test.events <- c("A","B","C","X") test.stm <- seqe2stm(test.events, dropList=list("A"=test.events[-1], B=test.events[-2], C=test.events[-3], X=test.events[-4])) test.tse <- data.frame(id = c(99,1,1,1), time = c(0,1,2,3), event = c("X","A","B","C")) test.sts <- TSE_to_STS(test.tse, id = "id", timestamp = "time", event = "event", stm=test.stm, tmin=1, tmax=4, firstState="None") test.seqdef <- seqdef(test.sts,informat = "STS") test.trate <- seqtrate(test.seqdef) test.trate[-nrow(test.trate),-ncol(test.trate)] 结合使用即可。你应该能够弄明白,解决这个问题。