如何编写 T-SQLhaving 查询?

发布于 2025-01-07 18:34:55 字数 544 浏览 0 评论 0原文

我正在使用 SSMS 2008,并尝试编写一个 TSQL 语句来仅返回每个“people_id”具有多个“intake_dt”值的记录。但下面这个查询似乎不起作用。我该如何重写这个?

SELECT el.people_id, el.actual_date
INTO #READMISSIONS
from event_log_rv el
 join enrollment_view en on en.event_log_id = el.event_log_id
 join (select actual_date from event_log_rv where actual_date is not null group by actual_date having count(*) > 1) t on t.actual_date = el.actual_date
where el.actual_date is not null 
group by el.people_id, el.actual_date

我没有收到任何错误,但输出不只显示一条记录/人。我想要的只是每人一条具有最新实际日期的记录。

I am using SSMS 2008 and am trying to write a TSQL statement to return only records with multiple "intake_dt" values per "people_id". But this query below doesn't seem to be working. How do I rewrite this?

SELECT el.people_id, el.actual_date
INTO #READMISSIONS
from event_log_rv el
 join enrollment_view en on en.event_log_id = el.event_log_id
 join (select actual_date from event_log_rv where actual_date is not null group by actual_date having count(*) > 1) t on t.actual_date = el.actual_date
where el.actual_date is not null 
group by el.people_id, el.actual_date

I am not getting any errors, but the output is not displaying just one record / person. All I want is one record per person with the latest actual_date.

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

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

发布评论

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

评论(1

十年不长 2025-01-14 18:34:55

这会给你你想要的结果吗?

    SELECT el.people_id,
    Max(el.actual_date) Max_Actual_Date
INTO #READMISSIONS
FROM event_log_rv el
INNER JOIN enrollment_view en
    ON en.event_log_id = el.event_log_id
INNER JOIN (
    SELECT actual_date
    FROM event_log_rv
    WHERE actual_date IS NOT NULL
    GROUP BY actual_date
    HAVING COUNT(*) > 1
    ) t
    ON t.actual_date = el.actual_date
GROUP BY el.people_id,

Does this give you the result you want?

    SELECT el.people_id,
    Max(el.actual_date) Max_Actual_Date
INTO #READMISSIONS
FROM event_log_rv el
INNER JOIN enrollment_view en
    ON en.event_log_id = el.event_log_id
INNER JOIN (
    SELECT actual_date
    FROM event_log_rv
    WHERE actual_date IS NOT NULL
    GROUP BY actual_date
    HAVING COUNT(*) > 1
    ) t
    ON t.actual_date = el.actual_date
GROUP BY el.people_id,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文