我想根据LastModifiedDate或CreatedDate对记录进行排序,我希望两者中最新的应该出现在顶部。 我尝试使用order by CreatedDate DESC, LastModifiedDate DESC
但这并没有在顶部显示其中的最新内容,它首先优先考虑上次修改日期,然后是创建日期
您可以比较 2 列中的 2 个日期,然后选择最新的日期
SELECT
CASE WHEN lastmodifiedDate > createdDate THEN lastmodifiedDate
ELSE createdDate
END AS LatestDate
FROM yourtable
对不起,我忘了你需要订购。然后你可以试试下面
select columns from yourtable order by CASE WHEN lastmodifiedDate > createdDate THEN lastmodifiedDate
ELSE createdDate
END AS LatestDate desc
您可以在 Order By 子句中使用 Case 语句,例如:
order by
case when CreatedDate > LastModifiedDate then CreatedDate else LastModifiedDate end DESC
DECLARE @A TABLE (ID INT,CREATION_DATE DATE,LAST_MODIFIED_DATE DATE)
INSERT INTO @A
VALUES
(1,'2016/06/06','2016/06/06'),
(2,'2016/06/06','2016/06/07'),
(3,'2016/06/06','2016/06/05')
SELECT S.* FROM
(
SELECT A.ID,A.CREATION_DATE,A.LAST_MODIFIED_DATE,
CASE
WHEN A.LAST_MODIFIED_DATE > A.CREATION_DATE THEN A.LAST_MODIFIED_DATE
ELSE A.CREATION_DATE
END AS ORDER_DATE
FROM @A A
) S
ORDER BY S.ORDER_DATE DESC