将一行中的值除以第二行中的值-基于相同的分组

时间:2019-10-02 13:34:23

标签: sql tsql sql-server-2016

我希望完成的工作是将飓风过后的每个月的未结清索赔除以未结清索赔。在下面的示例中,我显示了所需的输出-我试图将未结索赔的数量除以未结索赔的数量,以得到要显示在报表上的百分比。我的示例仅在“ C”行中显示了%闭合值-但是在“ O”打开行中是否也有相同的值也没关系-我可以使用行号功能来确保仅显示值一次出现在报告上。

我感谢所有答复-它们似乎都使我达到了期望的结果-只需找出哪种方法最适合我的情况即可。谢谢!

enter image description here

样本数据:

CREATE TABLE #Hurricanes
(
    Hurricane VARCHAR (50) NOT NULL,
    MonthNumber INT NOT NULL,
    CurrentClaimStatus CHAR (1) NOT NULL,
    ClaimCount INT NOT NULL
);

INSERT INTO #Hurricanes
(
    Hurricane,
    MonthNumber,
    CurrentClaimStatus,
    ClaimCount
)
VALUES
('Hurricane Michael', 1, 'C', 22),
('Hurricane Michael', 1, 'O', 344),
('Hurricane Michael', 2, 'C', 62),
('Hurricane Michael', 2, 'O', 354),
('Hurricane Michael', 3, 'C', 93),
('Hurricane Michael', 3, 'O', 357),
('Hurricane Michael', 4, 'C', 126),
('Hurricane Michael', 4, 'O', 365),
('Hurricane Michael', 5, 'C', 174),
('Hurricane Michael', 5, 'O', 369),
('Hurricane Michael', 6, 'C', 219),
('Hurricane Michael', 6, 'O', 369),
('Hurricane Michael', 7, 'C', 242),
('Hurricane Michael', 7, 'O', 372),
('Hurricane Michael', 8, 'C', 252),
('Hurricane Michael', 8, 'O', 373),
('Hurricane Michael', 9, 'C', 258),
('Hurricane Michael', 9, 'O', 373),
('Hurricane Michael', 10, 'C', 263),
('Hurricane Michael', 10, 'O', 376),
('Hurricane Michael', 11, 'C', 274),
('Hurricane Michael', 11, 'O', 377),
('Hurricane Michael', 12, 'C', 283),
('Hurricane Michael', 12, 'O', 377);

SELECT *
FROM #Hurricanes
ORDER BY Hurricane,
         MonthNumber,
         CurrentClaimStatus;

DROP TABLE #Hurricanes;

5 个答案:

答案 0 :(得分:1)

根据您的数据,您将获得所需的结果。显然,如果您需要越过几年,就必须对其进行一些修改

SELECT t1.Hurricane, t1.MonthNumber,t1.ClaimCount OpenCount, t2.ClaimCount CloseCount, t2.ClaimCount / (t1.ClaimCount * 1.0) [% Closed]
FROM #Hurricanes t1
INNER JOIN #Hurricanes t2 on t1.MonthNumber = t2.MonthNumber and t2.CurrentClaimStatus = 'C'
WHERE t1.CurrentClaimStatus = 'O'
ORDER BY Hurricane,
         MonthNumber

答案 1 :(得分:1)

如果我理解正确,那么您只想要累计金额:

SELECT Hurricane, MonthNumber,
       SUM(CASE WHEN CurrentClaimStatus = 'O' THEN ClaimCount ELSE 0 END) OVER (PARTITION BY Hurricane, MonthNumber) as openclaims,
       SUM(CASE WHEN CurrentClaimStatus = 'C' THEN ClaimCount ELSE 0 END) OVER (PARTITION BY Hurricane, MonthNumber) as closedclaims,
       (SUM(CASE WHEN CurrentClaimStatus = 'C' THEN ClaimCount ELSE 0.0 END) OVER (PARTITION BY Hurricane, MonthNumber) /
       SUM(CASE WHEN CurrentClaimStatus = 'O' THEN ClaimCount END) OVER (PARTITION BY Hurricane, MonthNumber)
      ) as ratio
FROM #Hurricanes h
ORDER BY Hurricane, MonthNumber;

答案 2 :(得分:1)

在窗口sum()中使用条件案例语句

[% Closed]  = case  when    CurrentClaimStatus = 'C' 
              then  
                    sum(case when CurrentClaimStatus = 'C' then ClaimCount end) over (partition by MonthNumber) * 1.0
                /   sum(case when CurrentClaimStatus = 'O' then ClaimCount end) over (partition by MonthNumber)
              end

答案 3 :(得分:1)

这种方法每月将数据旋转到一行:

WITH ClaimsPivot AS
(
SELECT 
Hurricane, MonthNumber,
SUM(IIF(CurrentClaimStatus = 'C', ClaimCount,NULL)) AS ClosedClaims,
SUM(IIF(CurrentClaimStatus = 'O', ClaimCount,NULL)) AS OpenClaims
FROM #Hurricanes
GROUP BY Hurricane,
         MonthNumber
)
SELECT 
Hurricane, MonthNumber,ClosedClaims,OpenClaims,
1.0 * ClosedClaims / OpenClaims AS ClaimsPct
FROM ClaimsPivot

答案 4 :(得分:1)

我认为您不需要row_number函数来解决您的问题...我还包括了年份...

查询

SELECT  h1.*, CASE WHEN h1.CurrentClaimStatus = 'C' THEN CAST(h1.ClaimCount * 1.0 / h2.ClaimCount AS varchar(25) ) ELSE '' END AS [%Closed]
FROM    #Hurricanes h1
    left outer join #Hurricanes h2 
       on h2.Hurricane = h1.Hurricane and h2.YearNumber = h1.YearNumber and h2.MonthNumber = h1.MonthNumber and h2.CurrentClaimStatus = 'O'

结果

Hurricane         | YearNumber | MonthNumber | Status | ClaimCount | %Closed
===================================================================================
Hurricane Michael | 2019       | 1           | C      | 22         | 0.063953488372
Hurricane Michael | 2019       | 1           | O      | 344        |
Hurricane Michael | 2019       | 2           | C      | 62         | 0.175141242937
Hurricane Michael | 2019       | 2           | O      | 354        |
Hurricane Michael | 2019       | 3           | C      | 93         | 0.260504201680
Hurricane Michael | 2019       | 3           | O      | 357        |
Hurricane Michael | 2019       | 4           | C      | 126        | 0.345205479452
Hurricane Michael | 2019       | 4           | O      | 365        |
...
...