使用几列SQL中的数据选择MAX日期

时间:2018-08-09 12:29:53

标签: sql sql-server date max

我知道这是一个经常被问到的问题,我已经浏览了已有的内容,但是我相信我的案子有点独特(如果没有,请指出正确的方向)。

我正在尝试查找与用户关联的行的最新出现,该行当前跨两个表和几列。

table: statusUpdate
+-------+-----------+-----------+-------------------+
|   id |    name    |   status  |       date_change |
+-------+-----------+-----------+-------------------+
|   1   |   Matt    |       0   |   01-01-2001      |
|   2   |   Jeff    |       1   |   01-01-2001      |
|   3   |   Jeff    |       2   |   01-01-2002      |
|   4   |   Bill    |       2   |   01-01-2001      |
|   5   |   Bill    |       3   |   01-01-2004      |
+-------+-----------+-----------+-------------------+

table: relationship
+-------+-----------+--------------+
|   id  |   userID  |stautsUpdateID|       
+-------+-----------+--------------+
|   1   |   22      |       1      |   
|   2   |   33      |       2      |   
|   3   |   33      |       3      |   
|   4   |   44      |       4      | 
|   5   |   44      |       5      | 
+-------+-----------+--------------+

第三个表将userID链接到它自己的表,但是这些示例表应该足以解决我的问题。

我希望按日期获取最新状态更改。当前的问题是它返回状态更改的所有实例。

Current results:
+-------+---------+-----------+-------------------+
|userID |statusID |   status  |       date_change |
+-------+---------+-----------+-------------------+
|   33  |   2     |       1   |   01-01-2001      |
|   33  |   3     |       2   |   01-01-2002      |
|   44  |   4     |       2   |   01-01-2001      |
|   44  |   5     |       3   |   01-01-2004      |
+-------+---------+-----------+-------------------+

Expected results:
+-------+-----------+-----------+-------------------+
|userID |statusID   |   status  |       date_change |
+-------+-----------+-----------+-------------------+
|   33  |   3       |       2   |   01-01-2002      |
|   44  |   5       |       3   |   01-01-2004      |
+-------+-----------+-----------+-------------------+

我希望一切都有意义,否则,请询问更多信息。

只需重申一下,我只想按日期返回用户状态更改的最新实例。

Sample code of one of my attempts:

select 
st.ID, st.status, st.date_change, r.userID 
from statusUpdate st
inner join Relationship r on st.ID = r.statusUpdateID

inner join (select ID, max(date_change) as recent from statusUpdate
group by ID) as y on r.stausUpdateID = y.ID and st.date_change = 
y.recent

希望有人可以指出我正确的方向。

2 个答案:

答案 0 :(得分:5)

使用row_number()按用户获取最后一行

select *
from
(
select st.ID, st.status, st.date_change, r.userID,
       rn = row_number() over (partition by r.userID order by st.date_change desc) 
from   statusUpdate st
       inner join Relationship r on st.ID = r.statusUpdateID
) as d
where rn  = 1

答案 1 :(得分:2)

我为您的答案添加了MAX条件

CREATE TABLE #Table1
        ([id] int, [name] varchar(4), [status] int, [date_change] datetime)
    ;

INSERT INTO #Table1
    ([id], [name], [status], [date_change])
VALUES
    (1, 'Matt', 0, '2001-01-01 00:00:00'),
    (2, 'Jeff', 1, '2001-01-01 00:00:00'),
    (3, 'Jeff', 2, '2002-01-01 00:00:00'),
    (4, 'Bill', 2, '2001-01-01 00:00:00'),
    (5, 'Bill', 3, '2004-01-01 00:00:00')
;


CREATE TABLE #Table2
    ([id] int, [userID] int, [stautsUpdateID] int)
;

INSERT INTO #Table2
    ([id], [userID], [stautsUpdateID])
VALUES
    (1, 22, 1),
    (2, 33, 2),
    (3, 33, 3),
    (4, 44, 4),
    (5, 44, 5)


select 
max(st.ID) id , max(st.status) status , max(st.date_change) date_change, r.userID 
from #Table1 st
inner join #Table2 r on st.ID = r.stautsUpdateID

inner join (select ID, max(date_change) as recent from #Table1
group by ID) as y on r.stautsUpdateID = y.ID and st.date_change = 
y.recent
group by  r.userID 

输出

id  status  date_change               userID
1   0       2001-01-01 00:00:00.000    22
3   2       2002-01-01 00:00:00.000    33
5   3       2004-01-01 00:00:00.000    44