SQL查询同时获取分组依据和不同值

发布于 2024-09-13 19:26:34 字数 598 浏览 4 评论 0原文

我在尝试为此表定义 SQL 查询时遇到问题:

有一个患者表及其在访视时记录的体重读数,其中包含以下列:

  • 患者 ID
  • 体重读数
  • 访视 ID(每次访视一个)

换句话说,如果分两次如果记录两个访问 ID 相同,则在同一访问日期读取了两个体重读数。

我有这样的查询“获取所有至少有两个体重读数高于 150 的患者”:

select patient_id 
  from patients 
 where weight_val > 50 
group by patient_id 
  having count(*) >= 2

这是我的问题:如果我想修改此查询以便我可以查询以下内容该怎么办:

  1. “获取所有至少有两个体重读数高于 150 的患者” 150 在不同的就诊中”
  2. “在同一次就诊中获得至少两个体重读数高于 150 的所有患者”

是否可以在不删除“group by”语句的情况下做到这一点?如果没有,您推荐的方法是什么?如果更容易的话,我也愿意添加日期列而不是访问 ID(我正在使用 Oracle)。

I'm having trouble trying to define the SQL query for this table:

There's a table of patients and their weight readings recorded on visits with the following columns:

  • patient ID
  • weight reading
  • visit ID (one per visit)

In other words, if in two records two visit IDs are the same, then two weight readings have been taken on that same visit date.

I have this query to "get all patients with at least two weight readings above 150":

select patient_id 
  from patients 
 where weight_val > 50 
group by patient_id 
  having count(*) >= 2

Here's my problem: What if I want to modify this query so that I can query the following:

  1. "get all patients with at least two weight readings above 150 on different visits"
  2. "get all patients with at least two weight readings above 150 on the same visit"

Is it possible to do it without removing the "group by" statement? if not, what is your recommended approach? I'm also open to adding a date column instead of visit ID if it makes it easier (i'm using Oracle).

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

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

发布评论

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

评论(2

死开点丶别碍眼 2024-09-20 19:26:34

不同就诊中至少有两次体重读数高于 150 的患者

使用:

  SELECT p.patient_id 
    FROM PATIENTS p
   WHERE p.weight_val > 150 
GROUP BY p.patient_id 
  HAVING COUNT(DISTINCT p.visit_id) >= 2

同一次就诊中至少两次体重读数高于 150 的患者

使用:

  SELECT DISTINCT p.patient_id 
    FROM PATIENTS p
   WHERE p.weight_val > 150 
GROUP BY p.patient_id, p.visit_id
  HAVING COUNT(*) >= 2

Patients with at least two weight readings above 150 on different visits

Use:

  SELECT p.patient_id 
    FROM PATIENTS p
   WHERE p.weight_val > 150 
GROUP BY p.patient_id 
  HAVING COUNT(DISTINCT p.visit_id) >= 2

Patients with at least two weight readings above 150 on the same visit

Use:

  SELECT DISTINCT p.patient_id 
    FROM PATIENTS p
   WHERE p.weight_val > 150 
GROUP BY p.patient_id, p.visit_id
  HAVING COUNT(*) >= 2
々眼睛长脚气 2024-09-20 19:26:34

尝试这样:

1.

select patient_id 
  from patients 
 where weight_val > 150 
group by patient_id 
  having count(*) >= 2 and count(*) = count(distinct visit_id);

2.

select patient_id 
  from patients 
 where weight_val > 150 
group by patient_id 
  having count(*) >= 2 and count(distinct visit_id) = 1;

try like this:

1.

select patient_id 
  from patients 
 where weight_val > 150 
group by patient_id 
  having count(*) >= 2 and count(*) = count(distinct visit_id);

2.

select patient_id 
  from patients 
 where weight_val > 150 
group by patient_id 
  having count(*) >= 2 and count(distinct visit_id) = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文