简体   繁体   English

合并多个Select查询

[英]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查询 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 output 格式如下

0, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl 0、ptp、bptp、fptp、待定、NC、anf、sfd、cb、lm、dl

Query 2:查询 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 output 格式如下

31, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl 31、ptp、bptp、fptp、待定、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 output 格式如下

bucket, ptp, bptp, fptp, pending, nc, anf,sfd,cb,lm, dl桶,ptp,bptp,fptp,待定,NC,anf,sfd,cb,lm,dl

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

31,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 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我试图将它们包装到另一个Select查询中,但它一直给我一个语法错误

Any advice?有什么建议吗?

Union is the answer.. silly me联盟是答案..傻我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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