如何在没有嵌套查询的情况下获取计数并在计算中使用该计数?
How to get count and use that count in a calculation with out nested query?
我正在尝试获取计数和使用该计数的比率。我尝试过的每个不使用嵌套查询的查询都会给我一个分组错误,因为在计算中使用计数的元组不能按表达式分组。
下面的查询为我提供了我正在寻找的数据。有没有一种方法可以将此查询编写为单个 select 而无需嵌套查询?
select
org,
format_number(distinct_preg,0),
format_number(total_patients,0),
format_number((distinct_preg/total_patients * 100),2) preg_rate
from (
select
preg.org,
count(distinct preg.patient_id) distinct_preg,
tot.total_patients total_patients
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
)
order by 1
---更新----------------------------
这似乎与format_number()函数有关。
此查询有效:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
tot.total_patients total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
这个给出了显示的错误,仅格式调用不同:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
如果你想要每个患者的平均怀孕次数,你可以使用:
select org, count(*) as num_pregnancies, count(distinct patient_id) as num_patients,
count(*) * 1.0 / count(distinct patient_id) as avg_pregnancies_per_patient
from pregnancies p
group by org;
没有明确说明您要做什么,这是一种猜测。但是,这似乎是一个非常合理的指标,您正在尝试计算。
显然,编号分组依据是问题所在,如果我将其更改为命名字段,它会起作用。
这个有效:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,tot.total_patients
这不是:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
我正在尝试获取计数和使用该计数的比率。我尝试过的每个不使用嵌套查询的查询都会给我一个分组错误,因为在计算中使用计数的元组不能按表达式分组。
下面的查询为我提供了我正在寻找的数据。有没有一种方法可以将此查询编写为单个 select 而无需嵌套查询?
select
org,
format_number(distinct_preg,0),
format_number(total_patients,0),
format_number((distinct_preg/total_patients * 100),2) preg_rate
from (
select
preg.org,
count(distinct preg.patient_id) distinct_preg,
tot.total_patients total_patients
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
)
order by 1
---更新----------------------------
这似乎与format_number()函数有关。
此查询有效:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
tot.total_patients total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
这个给出了显示的错误,仅格式调用不同:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3
如果你想要每个患者的平均怀孕次数,你可以使用:
select org, count(*) as num_pregnancies, count(distinct patient_id) as num_patients,
count(*) * 1.0 / count(distinct patient_id) as avg_pregnancies_per_patient
from pregnancies p
group by org;
没有明确说明您要做什么,这是一种猜测。但是,这似乎是一个非常合理的指标,您正在尝试计算。
显然,编号分组依据是问题所在,如果我将其更改为命名字段,它会起作用。
这个有效:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,tot.total_patients
这不是:
select
preg.org,
format_number(count(distinct preg.patient_id),0) distinct_preg,
format_number(tot.total_patients,0) total_patients,
format_number((count(distinct preg.patient_id) / tot.total_patients * 100),2) preg_rate
from
pregnancy preg
join (
select org, count(distinct patient_id) total_patients from enc group by 1
) tot on preg.org = tot.org
group by 1,3