我需要按照dateshipped的顺序设置项,但在CTE的中间。我使用一个窗口函数来设置项目的顺序(因为我不能使用order )。
select ROW_NUMBER() over(partition by [sku] order by [dateshipped] desc) as [rows]
,[sku]
,[dateshipped]
,[priceperunit]
from [Order Details]我遇到的问题是,当dateshipped为null时,没有为该项分配行号。
当dateshipped为null时,如何获得指定的行号?
发布于 2014-02-11 21:40:15
如果dateshipped是NULL,它们无论如何都会被排序,就像它是最早的datetime一样,就像在普通ORDER BY中使用可空列一样。但是,您可以首先使用CASE来指定空值是在前面还是最后一个:
SELECT Row_number()
OVER(
partition BY [sku]
ORDER BY CASE WHEN dateshipped IS NULL THEN 0 ELSE 1 END ASC,
dateshipped DESC) AS [rows],
[sku],
[dateshipped],
[priceperunit]
FROM [order details] https://stackoverflow.com/questions/21713536
复制相似问题