Oracle SQL - 基于一列独特的一个分数列汇总多个级别

时间:2016-04-07 21:59:22

标签: sql oracle

我想返回"分数"的SUM和SAMPLE SIZE。个人"患者"的多个汇总级别的列。问题在于,当我添加汇总级别时,其中一些汇总级别将每位患者的一行分成每个患者多行。由于这些患者被分开,概要级别的总和开始不准确。是否有某种方法可以使用一个子查询,每个子行数为#34;患者",但每个级别只有Group By / SUM WHERE"患者"是独一无二的吗?

子查询结果示例:

Patient Location    DiseaseState    Care Team Member    Score
1       CityA       Hypertension    Provider1       10
1       CityA       Hypertension    Pharmacist1     10
1       CityA       Hypertension    Health Coach1   10
1       CityA       Diabetes        Provider1       10
1       CityA       Diabetes        Pharmacist1     10
1       CityA       Diabetes        Health Coach1   10
2       CityB       Hypertension    Provider1       20
2       CityB       Hypertension    Pharmacist2     20
2       CityB       Hypertension    Health Coach1   20
2       CityB       Diabetes        Provider1       20
2       CityB       Diabetes        Pharmacist2     20
2       CityB       Diabetes        Health Coach1   20
2       CityB       CKD             Provider1       20
2       CityB       CKD             Pharmacist2     20
2       CityB       CKD             Health Coach1   20

我想从这个子查询中总结的是每个" Care Team Member"," DiseaseState"," Location"的平均分数。如果我和"得分"通过" Care Team Member",我获得了比我应该高得多的分数。我可以以某种方式评估"得分"在哪里"患者"是UNIQUE和GROUP BY"位置","疾病状态","护理团队成员"等?

1 个答案:

答案 0 :(得分:0)

您应该能够将PARTITION与SUM分析功能一起使用。类似的东西:

SELECT SUM(SCORE) OVER (PARTITION BY "Location", "DiseaseState", "Care Team Member")
FROM TABLE

partition子句的作用类似于group by,只对每个组的值求和,但不要求您修改现有的group by子句

以下链接包含一些使用SUM作为分析函数的附加信息和示例 http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions182.htm#SQLRF06115