众多工会,更轻松的方式?

时间:2017-05-04 18:32:42

标签: sql sql-server

我正在使用下面的查询并获得我想要的结果,我只是不禁认为有可能比使用不同的where子句一遍又一遍地重复代码更好的方法?建议?

WITH HHSUMMARY
AS
(
SELECT
    B.HouseholdId, SUM(B.Balance) AS AUM, SUM(B.TradeRevenue + B.TrailRevenue + B.AdvisoryRevenue) AS Revenue

    FROM
        BookSegmentation.BookSegmentationDataset B

    WHERE
        B.RepName = 'Rep'

    GROUP BY
        B.HouseholdId
)

SELECT
    '< 25K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM < 25000

UNION

SELECT
    '25K-50K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 25000 AND AUM < 50000

UNION

SELECT
    '50K-100K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 50000 AND AUM < 100000

UNION

SELECT
    '100K-250K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 100000 AND AUM < 250000

UNION

SELECT
    '250K-500K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 250000 AND AUM < 500000

UNION

SELECT
    '500K-750K' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 500000 AND AUM < 750000

UNION

SELECT
    '750K-1MM' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 750000 AND AUM < 1000000

UNION

SELECT
    '1MM-5MM' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 1000000 AND AUM < 5000000

UNION

SELECT
    '> 5MM' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY
    WHERE AUM >= 5000000

UNION

SELECT
    'TOTAL' AS [ACCT BALANCE CAT],
    COUNT(HouseholdID) AS HH,
    CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
    SUM(AUM) AS [HH AUM],
    SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
    SUM(Revenue) AS REVENUE,
    SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
    SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
    FROM HHSUMMARY

这是我的结果:

enter image description here

1 个答案:

答案 0 :(得分:6)

是的,还有更好的方法。您可以在CTE中定义组,然后将该组与GROUP BY

一起使用
WITH HHSUMMARY AS (
      SELECT B.HouseholdId, SUM(B.Balance) AS AUM,
             SUM(B.TradeRevenue + B.TrailRevenue + B.AdvisoryRevenue) AS Revenue,
             (CASE WHEN SUM(B.BALANCE) < 25000 THEN '<25000'
                   WHEN SUM(B.BALANCE) < 50000 THEN '25K-50K'
                   . . .
              END) as [ACCT BALANCE CAT]
      FROM BookSegmentation.BookSegmentationDataset B
      WHERE B.RepName = 'Rep'
      GROUP BY B.HouseholdId
    )
SELECT COALESCE([ACCT BALANCE CAT], 'Total'),
       COUNT(HouseholdID) AS HH,
       CAST(COUNT(HouseholdId) AS DECIMAL(18,2))/(SELECT CAST(COUNT(HouseholdId) AS DECIMAL(18,2)) FROM HHSUMMARY) AS [% HH],
       SUM(AUM) AS [HH AUM],
       SUM(AUM)/(SELECT SUM(AUM) FROM HHSUMMARY) AS [HH AUM %],
       SUM(Revenue) AS REVENUE,
       SUM(Revenue)/(SELECT SUM(Revenue) FROM HHSUMMARY) AS [% REV],
       SUM(Revenue)/SUM(AUM) AS [EFFICIENCY %]
FROM HHSUMMARY
GROUP BY GROUPING SETS ( ([ACCT BALANCE CAT]), () );

请注意使用GROUPING SETS来计算总数。您也可以使用ROLLUP