SQL在运行总计上运行total或Case语句

时间:2017-01-20 13:34:19

标签: sql sql-server sql-server-2000

我有一个如下所示的查询:

SELECT b.HH,
       b.TotalRevenues,
       b.TotalOperatingExpenses,
       b.TotalAUA,
       (b.TotalRevenues -b.TotalOperatingExpenses) AS Profit,
       ((b.TotalRevenues -b.TotalOperatingExpenses)/sum((b.TotalRevenues -b.TotalOperatingExpenses))) AS percentProfit FROM
  ( SELECT * FROM
     (SELECT CASE
                 WHEN (LTRIM(RTRIM(v.ISM_HH_KEY)) <> '')--OR
 --(LTRIM(RTRIM(v.ISM_HH_KEY)) IS NOT NULL)
 THEN LTRIM(RTRIM(v.ISM_HH_KEY))
                 ELSE LEFT(LTRIM(RTRIM(v.ACCOUNT)), 6)
             END AS 'HH', [WealthDB].[dbo].[WF_PM_SUM_DB].account AS 'Account', (sum(DB_1_1) + sum(DB_1_2) + sum(DB_1_3) + sum(DB_1_5) + sum(DB_2_1_1) + sum(DB_2_1_2) + sum(DB_2_2_1) + sum(DB_2_2_2)+SUM(DB_3_1)) AS 'TotalRevenues', SUM(DB_4) AS 'TotalOperatingExpenses', AVG(DB_10) AS 'TotalAUA'
      FROM [WealthDB].[dbo].[WF_PM_SUM_DB]
      INNER JOIN
        (SELECT DISTINCT ISM_HH_Key, account, Municipality
         FROM [WealthDB].[dbo].[WF_PM_ACCT_det_DB] ) AS v ON [WealthDB].[dbo].[WF_PM_SUM_DB].Account = v.Account
      WHERE [WealthDB].[dbo].[WF_PM_SUM_DB].yearmonth > '201512'
      GROUP BY v.account, V.ISM_HH_Key,[WealthDB].[dbo].[WF_PM_SUM_DB].account, v.Municipality --   (
 ) a
   GROUP BY a.HH, a.Account, a.HH, a.TotalRevenues, a.TotalOperatingExpenses,a.TotalAUA
   HAVING a.TotalAUA > 0
   AND a.TotalRevenues>0 ) b
GROUP BY b.HH,
         b.TotalRevenues,
         b.TotalOperatingExpenses,
         b.TotalAUA
HAVING (b.TotalRevenues -b.TotalOperatingExpenses) > 0
ORDER BY profit ASC

我得到一个看起来像

的输出
Account     Revenue       Expenses   Profit     PercentageProfit
XXXX        $100,000       $20000    $15,000           1
YYY         $90000         $20000    $14,000           1

percaentage利润没有正确计算,因为它在每一行显示1,而且我想要的是另一列,它给出了另一列中的百分比利润的累计总计,如:

Account     Revenue       Expenses   Profit     PercentageProfit  RunningTotal
XXXX        $100,000       $20000    $15,000           0.32%          0.32%
YYY         $90000         $20000    $14,000           0.29%          0.61%

我使用的SQL服务器是2000版,所以我不能使用秩功能。有什么建议。我不认为我可以使用OVER条款,所以我不能在利润列上做一个无限的总和,以获得百分比的利润。

此外,一旦我们有一个运行的总列,我可以使用像

这样的case语句
CASE WHEN RUNNINGTOTAL < 1% THEN 'TOP 1%'
CASE WHEN RUNNINGTOTAL >1% AND <20%   THEN '1-20%'
END AS 'SEGMENT'

1 个答案:

答案 0 :(得分:0)

((b.TotalRevenues -b.TotalOperatingExpenses)/sum((b.TotalRevenues -b.TotalOperatingExpenses)))

不会给你百分比,因为TotalRevenuesTotalOperatingExpenses都是group by的一部分所以它永远不会给你Profit总计算百分比

尝试这样的事情

;WITH cte
     AS (SELECT b.HH,
                b.TotalRevenues,
                b.TotalOperatingExpenses,
                b.TotalAUA,
                ( b.TotalRevenues - b.TotalOperatingExpenses )                AS Profit,
                ( ( b.TotalRevenues - b.TotalOperatingExpenses ) / Sum(( b.TotalRevenues - b.TotalOperatingExpenses ))
                                                                     OVER() ) AS percentProfit
         FROM   (<<Inner query>>) b
         WHERE  ( b.TotalRevenues - b.TotalOperatingExpenses ) > 0)
SELECT *,
       Sum(percentProfit)OVER(ORDER BY profit desc) as RunningTotal
FROM   cte
ORDER  BY profit ASC 

如果您使用的是旧版本的sql server,请将最终Select替换为

SELECT *,
       RunningTotal
FROM   cte
CROSS apply (SELECT Sum(percentProfit)
            FROM   cte b
            WHERE  b.percentProfit >= a.percentProfit) cs (RunningTotal) 
ORDER  BY profit ASC 

SQL SERVER 2000

SELECT *,
       (SELECT Sum(percentProfit)
        FROM   (SELECT b.HH,
                       b.TotalRevenues,
                       b.TotalOperatingExpenses,
                       b.TotalAUA,
                       ( b.TotalRevenues - b.TotalOperatingExpenses )                AS Profit,
                       ( ( b.TotalRevenues - b.TotalOperatingExpenses ) / (Select Sum(( b.TotalRevenues - b.TotalOperatingExpenses )) from (<<Inner query>>))) AS percentProfit
                FROM   (<<Inner query>>) b
                WHERE  ( b.TotalRevenues - b.TotalOperatingExpenses ) > 0) b
        WHERE  b.percentProfit >= a.percentProfit) RunningTotal
FROM   (SELECT b.HH,
               b.TotalRevenues,
               b.TotalOperatingExpenses,
               b.TotalAUA,
               ( b.TotalRevenues - b.TotalOperatingExpenses )                AS Profit,
               ( ( b.TotalRevenues - b.TotalOperatingExpenses ) / (Select Sum(( b.TotalRevenues - b.TotalOperatingExpenses )) from (<<Inner query>>))) AS percentProfit
        FROM   (<<Inner query>>) b
        WHERE  ( b.TotalRevenues - b.TotalOperatingExpenses ) > 0) a