我有这个问题:
SELECT *
FROM (SELECT *
FROM EventLog AS el
RIGHT JOIN (SELECT Min(id) AS gId
FROM EventLog
WHERE Value IS NOT NULL
GROUP BY Value) AS e
ON el.Id = e.gId) AS el
LEFT JOIN (SELECT userid AS uId,
Max([timestamp]) AS ts
FROM UserAdditionStamp
GROUP BY UserId) AS ua
ON el.UserId = ua.uId
WHERE el.EventTypeId = 3
从中选择的示例数据是:
Id UserId ts TimeStamp
-----------------------------------------------
461 1 2014-11-17 10:01:27.443 2013-01-08 16:28:52.557
468 25 NULL 2013-01-08 17:06:15.967
441 1 2014-11-17 10:01:27.443 2013-01-09 11:00:43.947
461 29 2013-01-05 16:28:52.557 2013-01-09 13:29:19.143
如何比较ts
和TimeStamp
日期?如何获得哪一个是最大日期?
主要想法是,如果ts
小于TimeStamp
,我应该在另一个表中插入数据。
答案 0 :(得分:1)
SELECT *, CASE WHEN coalesce(timestamp,'01/01/1901')>coalesce(ts,'01/01/1901') THEN 1 ELSE 0 END TimeStampBigger
FROM (SELECT *
FROM EventLog AS el
RIGHT JOIN (SELECT Min(id) AS gId
FROM EventLog
WHERE Value IS NOT NULL
GROUP BY Value) AS e
ON el.Id = e.gId) AS el
LEFT JOIN (SELECT userid AS uId,
Max([timestamp]) AS ts
FROM UserAdditionStamp
GROUP BY UserId) AS ua
ON el.UserId = ua.uId
WHERE el.EventTypeId = 3
TimeStamp
和ts
都可以作为外部select
中的列名提供,因此可以进行比较。作为额外列中的输出(如上所述:TimeStampBigger
)或where
子句中的另一个条件。