简体   繁体   中英

Combine Multiple Select Query

Experts,

I have the below two (actualy more than two) queries which i need to run in a single execution and get their results to avoid the execution cost.

Query 1

SELECT  0,                
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'PTP') as 'PTP',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'BPTP') as 'BPTP',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'PENDING') as 'Pending',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'NC') as 'NC',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'ANF') as 'ANF',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'SFD') as 'SFD',
                                (SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'CB') as 'CB',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'LM') as 'LM',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 0
                                AND l.allocationDPD <= 30) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'DL') as 'DL'

The output of this is in the below format

0, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl

Query 2:

SELECT  31,                
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'PTP') as 'PTP',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'BPTP') as 'BPTP',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'PENDING') as 'Pending',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'NC') as 'NC',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'ANF') as 'ANF',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'SFD') as 'SFD',
                                (SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'CB') as 'CB',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'LM') as 'LM',
(SELECT count(1)/(SELECT count(1) from LOANEE l WHERE l.allocationDPD >= 31
                                AND l.allocationDPD <= 60) * 100 FROM LOAN_TRANSACTION lt where lt.dispositionCode = 'DL') as 'DL'

The output of this is in the below format

31, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl

What I wanna do is combine a couple of these queries and return the data something like this

The output of this is in the below format

bucket, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl

0,1,2,3,4,5,6,7,8,9,10,11

31,1,2,3,4,5,6,7,8,9,10,11

61,1,2,3,4,5,6,7,8,9,10,11

I tried to wrap them into another Select query but it keeps giving me a syntax error

Any advice?

Union is the answer.. silly me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM