sql 按日期 取数值最大的
答案:1 悬赏:60 手机版
解决时间 2021-02-08 18:43
- 提问者网友:太高姿态
- 2021-02-08 10:58
sql 按日期 取数值最大的
最佳答案
- 五星知识达人网友:白昼之月
- 2021-02-08 12:31
给你个思路:
WITH Messages (ID,ToEmpID,FromEmpID,SendDate,Detail) AS
(
SELECt 1,'张三','李四','2014/06/05','a' UNIOn ALL
SELECT 2,'王五','张三','2014/06/03','b' UNIOn ALL
SELECT 3,'刘六','张三','2014/06/02','b' UNIOn ALL
SELECT 4,'张三','李四','2014/05/05','a' UNIOn ALL
SELECT 5,'王五','张三','2014/05/03','b' UNIOn ALL
SELECT 6,'刘六','张三','2014/05/02','b'
)
SELECT ID,ToEmpID,FromEmpID,SendDate FROM Messages a
WHERe ID in
(
SELECt TOP 1 ID
FROM Messages
where ToEmpID=a.ToEmpID AND FromEmpID=a.FromEmpID
ORDER BY SendDate DESC
)
WITH Messages (ID,ToEmpID,FromEmpID,SendDate,Detail) AS
(
SELECt 1,'张三','李四','2014/06/05','a' UNIOn ALL
SELECT 2,'王五','张三','2014/06/03','b' UNIOn ALL
SELECT 3,'刘六','张三','2014/06/02','b' UNIOn ALL
SELECT 4,'张三','李四','2014/05/05','a' UNIOn ALL
SELECT 5,'王五','张三','2014/05/03','b' UNIOn ALL
SELECT 6,'刘六','张三','2014/05/02','b'
)
SELECT ID,ToEmpID,FromEmpID,SendDate FROM Messages a
WHERe ID in
(
SELECt TOP 1 ID
FROM Messages
where ToEmpID=a.ToEmpID AND FromEmpID=a.FromEmpID
ORDER BY SendDate DESC
)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯