如何编写T-SQL有查询?

时间:2012-02-22 19:34:10

标签: having sql-server-2008

我正在使用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

我没有收到任何错误,但输出不会只显示一个记录/人。我想要的只是每人一张最新的actual_date记录。

1 个答案:

答案 0 :(得分:0)

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

    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,