-1

次の図の Report from 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 エンティティの計算も実行できます (既に追加されている - 他の列の合計)。同様に、追加の列 (合計、% など) を計算できます。この場合、レポートに必要なすべての計算データを返すクエリが 1 つあるだけです。

同じクエリ内でsum(MyOutStanding)計算する必要があるため、ピボットは使用できません。count(NoofFile)

楽しみ。

于 2013-11-06T09:51:13.617 に答える