如何对 sql 中的两个聚合字段求和
How to sum two aggregated fields in sql
我想得到以下信息:
- 患者人数
- 索赔数量
- 总计数(即 Patient_Count + Claim_Count)
select year,
count(distinct patientid) as Patitent_Count,
Count(distinct CLAIMID) as Claims_Count,
sum(Patitent_Count + Claims_Count) as Total_count
from sand_scipher_ds_db.ATS.sym
group by year
order by year
错误:
SQL compilation error: Aggregate functions cannot be nested: [COUNT(DISTINCT SYM.PATIENTID)] nested in [SUM(PATITENT_COUNT + CLAIMS_COUNT)]*
我还尝试了以下子查询:
select x.*,
sum(x.Patitent_Count + x.Number_of_claim) as Total_count
from(
select year, count(distinct patientid) as Patitent_Count, Count(distinct CLAIMID) as Number_of_claim from sand_scipher_ds_db.ATS.sym
group by year
order by year)x
group by year
order by year
但是仍然出现同样的错误
任何人都可以建议一种方法吗?
您可以使用子查询来做到这一点:
select year, Patitent_Count, Claims_Count, Patitent_Count + Claims_Count as Total_Count
from(select year,
count(distinct patientid) as Patitent_Count,
Count(distinct CLAIMID) as Claims_Count
from sand_scipher_ds_db.ATS.sym
group by year) t
order by year
你甚至不需要子查询:
select
year,
count(distinct patientid) Patitent_Count,
count(distinct CLAIMID) Claims_Count,
count(distinct patientid) + count(distinct CLAIMID) as Total_Count
from sand_scipher_ds_db.ATS.sym
group by year
order by year
在 snowflake 中更容易,只需通过您命名输出列的别名将它们相加即可。 Snowflake 见 total_count
是从两个聚合函数结果派生的,因此不需要添加到 GROUP BY 子句中。相当整洁。
SELECT
year,
COUNT(DISTINCT patientid) AS patitent_count,
COUNT(DISTINCT claimid) AS claims_count,
patitent_count + claims_count AS total_count
FROM sand_scipher_ds_db.ats.sym
GROUP BY 1
ORDER BY 1;
在其他数据库中,您已经像在 eshirvana 答案中那样写了相同的 SQL 2 次以上,或者使用子查询。
我想得到以下信息:
- 患者人数
- 索赔数量
- 总计数(即 Patient_Count + Claim_Count)
select year,
count(distinct patientid) as Patitent_Count,
Count(distinct CLAIMID) as Claims_Count,
sum(Patitent_Count + Claims_Count) as Total_count
from sand_scipher_ds_db.ATS.sym
group by year
order by year
错误:
SQL compilation error: Aggregate functions cannot be nested: [COUNT(DISTINCT SYM.PATIENTID)] nested in [SUM(PATITENT_COUNT + CLAIMS_COUNT)]*
我还尝试了以下子查询:
select x.*,
sum(x.Patitent_Count + x.Number_of_claim) as Total_count
from(
select year, count(distinct patientid) as Patitent_Count, Count(distinct CLAIMID) as Number_of_claim from sand_scipher_ds_db.ATS.sym
group by year
order by year)x
group by year
order by year
但是仍然出现同样的错误 任何人都可以建议一种方法吗?
您可以使用子查询来做到这一点:
select year, Patitent_Count, Claims_Count, Patitent_Count + Claims_Count as Total_Count
from(select year,
count(distinct patientid) as Patitent_Count,
Count(distinct CLAIMID) as Claims_Count
from sand_scipher_ds_db.ATS.sym
group by year) t
order by year
你甚至不需要子查询:
select
year,
count(distinct patientid) Patitent_Count,
count(distinct CLAIMID) Claims_Count,
count(distinct patientid) + count(distinct CLAIMID) as Total_Count
from sand_scipher_ds_db.ATS.sym
group by year
order by year
在 snowflake 中更容易,只需通过您命名输出列的别名将它们相加即可。 Snowflake 见 total_count
是从两个聚合函数结果派生的,因此不需要添加到 GROUP BY 子句中。相当整洁。
SELECT
year,
COUNT(DISTINCT patientid) AS patitent_count,
COUNT(DISTINCT claimid) AS claims_count,
patitent_count + claims_count AS total_count
FROM sand_scipher_ds_db.ats.sym
GROUP BY 1
ORDER BY 1;
在其他数据库中,您已经像在 eshirvana 答案中那样写了相同的 SQL 2 次以上,或者使用子查询。