-1

我需要如下图所示的来自 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
4

2 回答 2

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 实体的计算(如已添加的 - 一些其他列的总和)。以同样的方式可以计算附加列(如 Total、% 等)。在这种情况下,您只需一个查询即可返回报告所需的所有计算数据。

Pivot 因为需要计算sum(MyOutStanding)而不能count(NoofFile)在同一个查询中使用。

享受。

于 2013-11-06T09:51:13.617 回答
-1

使用 UNION ALL 将 6 个查询连接成一个查询。

于 2013-11-05T13:27:04.747 回答