Sql server 2008 r2 基于多表查询的查询设计

Sql server 2008 r2 基于多表查询的查询设计,sql-server-2008-r2,Sql Server 2008 R2,我需要的查询如下图片的报告从6查询:![输入图像] 此处有说明][1] [1] : 我的问题#1 我的问题2 我的问题#3 我的问题4 我的问题#5 我的问题#6 使用UNION ALL将6个查询合并到一个查询中。UNION ALL不是一个选项,因为您需要将每个状态的聚合结果分开,并将它们合并到一行中 如果状态量是静态的,则接收所需状态的最简单方法如下: select LoanType, sum(MyOutStanding_BL) as MyOutStanding_BL,

我需要的查询如下图片的报告从6查询:![输入图像] 此处有说明][1]

[1] :

我的问题#1

我的问题2

我的问题#3

我的问题4

我的问题#5

我的问题#6


使用UNION ALL将6个查询合并到一个查询中。

UNION ALL
不是一个选项,因为您需要将每个状态的聚合结果分开,并将它们合并到一行中

如果状态量是静态的,则接收所需状态的最简单方法如下:

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实体的计算(如已添加的—某些其他列的总和)。同样,可以计算附加列(如总计、%等)。在本例中,您只需要一个查询,它返回报表所需的所有计算数据

无法使用Pivot,因为需要在同一查询中计算
总和(MyOutStanding)
计数(NoofFile)


享受。

UNION ALL是一个选项,因为可以在Excel中毫无问题地处理数据透视。
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
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
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
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
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
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