如何获得价值与数量不同的人的加权总和

发布于 2025-01-21 05:53:36 字数 589 浏览 4 评论 0原文

我有一个没有给出预期结果的SQL查询:

CREATE TEMP TABLE tmp_cond_num as
          SELECT DISTINCT
          a.age
          ,a.gender
          ,b.concept_id
          ,count(distinct a.person_id) as numpat
          ,round(sum(a.sample_weight)) as w_numpat
          FROM tmp_pat a
          JOIN analytics.aia_tools.dh_t20_medical_histroy_w_hrc b ON a.person_id = b.person_id
          AND a.grp = 1
          GROUP BY
          a.age
          ,a.gender
          ,concept_id

我在这里计算独特的person_id。我需要计算那些独特的人_id的样本权重。现在,W_NUMPAT也正在总结重复的Person_ID。我如何获得独特的person_id的总和?

I have a sql query that is not giving the expected result:

CREATE TEMP TABLE tmp_cond_num as
          SELECT DISTINCT
          a.age
          ,a.gender
          ,b.concept_id
          ,count(distinct a.person_id) as numpat
          ,round(sum(a.sample_weight)) as w_numpat
          FROM tmp_pat a
          JOIN analytics.aia_tools.dh_t20_medical_histroy_w_hrc b ON a.person_id = b.person_id
          AND a.grp = 1
          GROUP BY
          a.age
          ,a.gender
          ,concept_id

I am counting distinct person_id here. I need to count the sample weights for those distinct person_id. Right now w_numpat is summing for duplicate person_id too. How do I just get the sum for the distinct person_id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2025-01-28 05:53:36

需要创建包含不同人结果的子查询。

CREATE TEMP TABLE tmp_cond_num as
          SELECT DISTINCT
          a.age
          ,a.gender
          ,b.concept_id
          ,count(a.person_id) as numpat
          ,sum(a.sample_weight) as w_numpat
          FROM tmp_pat a
          JOIN (SELECT distinct person_id, concept_id 
          FROM analytics.aia_tools.dh_t20_medical_histroy_w_hrc where first_inpatient_visit_date IS NOT NULL) b 
          ON a.person_id = b.person_id
          AND a.grp = 1
          GROUP BY
          a.age
          ,a.gender
          ,concept_id;

Need to create subquery that contains distinct person results.

CREATE TEMP TABLE tmp_cond_num as
          SELECT DISTINCT
          a.age
          ,a.gender
          ,b.concept_id
          ,count(a.person_id) as numpat
          ,sum(a.sample_weight) as w_numpat
          FROM tmp_pat a
          JOIN (SELECT distinct person_id, concept_id 
          FROM analytics.aia_tools.dh_t20_medical_histroy_w_hrc where first_inpatient_visit_date IS NOT NULL) b 
          ON a.person_id = b.person_id
          AND a.grp = 1
          GROUP BY
          a.age
          ,a.gender
          ,concept_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文