我需要查询如下图6的查询报告:![输入图像 这里的描述] [1]
[1]:http://i.stack.imgur.com/Lcsiz.png
我的查询#1
SELECT LoanType, SUM(MyOutStanding) AS MyOutStanding, COUNT(NoofFile) AS NoofFile, Status
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='Regular'
GROUP BY LoanType, Status order by LoanType, Status
我的查询#2
SELECT LoanType As UC_LoanType, SUM(MyOutStanding) AS UC_Out, COUNT(NoofFile) AS UC_file, Reg_MyOutStanding='',Reg_NoofFile=''
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='UC'
GROUP BY LoanType, Status order by LoanType, Status
我的查询#3
SELECT LoanType, SUM(MyOutStanding) AS MyOutStanding, COUNT(NoofFile) AS NoofFile, Status
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='SMA'
GROUP BY LoanType, Status order by LoanType, Status
我的查询#4
SELECT LoanType, SUM(MyOutStanding) AS MyOutStanding, COUNT(NoofFile) AS NoofFile, Status
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='SS'
GROUP BY LoanType, Status order by LoanType, Status
我的查询#5
SELECT LoanType, SUM(MyOutStanding) AS MyOutStanding, COUNT(NoofFile) AS NoofFile, Status
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='DF'
GROUP BY LoanType, Status order by LoanType, Status
我的查询#6
SELECT LoanType, SUM(MyOutStanding) AS MyOutStanding, COUNT(NoofFile) AS NoofFile, Status
FROM (SELECT (CASE WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan' WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END) AS LoanType,
SUM(outstanding) AS MyOutStanding, COUNT(clid) AS NoofFile, Status
FROM dbo.RecTestCL GROUP BY loantype, Status) X where Status='BL'
GROUP BY LoanType, Status order by LoanType, Status
答案 0 :(得分:1)
UNION ALL
不是一个选项,因为您需要将每个状态的聚合结果分开并将它们统一到1行。
如果状态数量是静态的,接收所需内容的最简单方法如下:
select
LoanType,
sum(MyOutStanding_BL) as MyOutStanding_BL,
sum(NoofFile_BL) as NoofFile_BL,
sum(MyOutStanding_DF) as MyOutStanding_DF,
sum(NoofFile_DF) as NoofFile_DF,
sum(MyOutStanding_SS) as MyOutStanding_SS,
sum(NoofFile_SS) as NoofFile_SS,
sum(MyOutStanding_SMA) as MyOutStanding_SMA,
sum(NoofFile_SMA) as NoofFile_SMA,
sum(MyOutStanding_UC) as MyOutStanding_UC,
sum(NoofFile_UC) as NoofFile_UC,
sum(MyOutStanding_Regular) as MyOutStanding_Regular,
sum(NoofFile_Regular) as NoofFile_Regular,
Reg_MyOutStanding='',Reg_NoofFile='',
sum(MyOutStanding_SS + MyOutStanding_DF + MyOutStanding_BL) as MyOutStanding_CL,
sum(NoofFile_SS + NoofFile_DF + NoofFile_BL) as NoofFile_CL
from
(
SELECT
LoanType,
case when Status = 'BL' then SUM(MyOutStanding) else 0 end AS MyOutStanding_BL,
case when Status = 'BL' then COUNT(NoofFile) else 0 end AS NoofFile_BL,
case when Status = 'DF' then SUM(MyOutStanding) else 0 end AS MyOutStanding_DF,
case when Status = 'DF' then COUNT(NoofFile) else 0 end AS NoofFile_DF,
case when Status = 'SS' then SUM(MyOutStanding) else 0 end AS MyOutStanding_SS,
case when Status = 'SS' then COUNT(NoofFile) else 0 end AS NoofFile_SS,
case when Status = 'SMA' then SUM(MyOutStanding) else 0 end AS MyOutStanding_SMA,
case when Status = 'SMA' then COUNT(NoofFile) else 0 end AS NoofFile_SMA,
case when Status = 'UC' then SUM(MyOutStanding) else 0 end AS MyOutStanding_UC,
case when Status = 'UC' then COUNT(NoofFile) else 0 end AS NoofFile_UC,
case when Status = 'Regular' then SUM(MyOutStanding) else 0 end AS MyOutStanding_Regular,
case when Status = 'Regular' then COUNT(NoofFile) else 0 end AS NoofFile_Regular
FROM
(
SELECT
CASE
WHEN loantype = 'Auto Loan' THEN 'Auto Loan'
WHEN loantype = 'Home Loan' THEN 'Home Loan'
WHEN loantype = 'Home Mortgage Loan' THEN 'Home Mortgage Loan'
ELSE 'Any Purpose Loan' END
AS LoanType,
SUM(outstanding) AS MyOutStanding,
COUNT(clid) AS NoofFile,
Status
FROM dbo.RecTestCL
GROUP BY loantype, Status
) X
GROUP BY LoanType, Status
) rez
group by LoanType
在这种情况下,您还可以执行CL实体的计算(已添加 - 其他一些列的总和)。以相同的方式可以计算附加列(如总计,%等)。在这种情况下,您只需一个查询即可返回报告所需的所有计算数据。
由于必须在同一查询中计算sum(MyOutStanding)
和count(NoofFile)
,因此无法使用数据透视表。
享受。
答案 1 :(得分:-1)
使用UNION ALL将6个查询加入单个查询中。