SQL查询与Row_Number不同
我正在与sql
的独特的关键字战斗。 我只想显示列中的所有唯一( distinct
)值的行号,所以我试过了:
SELECT distinct id, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM table where fid = 64
但是下面的代码给了我distinct
值:
SELECT distinct id FROM table where fid = 64
但用Row_Number
尝试时。
那么它不工作…
任何帮助,将不胜感激
感谢名单
用这个:
SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM (SELECT DISTINCT id FROM table WHERE fid = 64) Base
并把查询的“输出”作为另一个的“input”。
使用CTE:
; WITH Base AS ( SELECT DISTINCT id FROM table WHERE fid = 64 ) SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM Base
这两个查询应该是等价的。
技术上你可以
SELECT DISTINCT id, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS RowNum FROM table WHERE fid = 64
但是如果增加DISTINCT字段的数量,则必须将所有这些字段放在PARTITION BY
,例如
SELECT DISTINCT id, description, ROW_NUMBER() OVER (PARTITION BY id, description ORDER BY id) AS RowNum FROM table WHERE fid = 64
我甚至希望你明白,在这里违背标准的命名约定, id
应该是一个主键,按照定义是唯一的,所以DISTINCT
将是无用的,除非你把查询和JOIN
s / UNION ALL
。 ..
这可以做得很简单,你已经很接近了
SELECT distinct id, DENSE_RANK() OVER (ORDER BY id) AS RowNum FROM table WHERE fid = 64
本文介绍ROW_NUMBER()
和DENSE_RANK()
( RANK()
函数没有特别处理) 之间有趣的关系 。 当您需要在SELECT DISTINCT
语句中生成ROW_NUMBER()
时, ROW_NUMBER()
将在用DISTINCT
关键字删除之前生成不同的值。 例如这个查询
SELECT DISTINCT v, ROW_NUMBER() OVER (ORDER BY v) row_number FROM t ORDER BY v, row_number
…可能会产生这个结果( DISTINCT
不起作用):
+---+------------+ | V | ROW_NUMBER | +---+------------+ | a | 1 | | a | 2 | | a | 3 | | b | 4 | | c | 5 | | c | 6 | | d | 7 | | e | 8 | +---+------------+
而这个查询:
SELECT DISTINCT v, DENSE_RANK() OVER (ORDER BY v) row_number FROM t ORDER BY v, row_number
…在这种情况下产生你可能想要的东西:
+---+------------+ | V | ROW_NUMBER | +---+------------+ | a | 1 | | b | 2 | | c | 3 | | d | 4 | | e | 5 | +---+------------+
请注意, DENSE_RANK()
函数的ORDER BY
子句将需要SELECT DISTINCT
子句中的所有其他列才能正常工作。
所有三个function相比较
使用PostgreSQL / Sybase / SQL标准语法( WINDOW
子句):
SELECT v, ROW_NUMBER() OVER (window) row_number, RANK() OVER (window) rank, DENSE_RANK() OVER (window) dense_rank FROM t WINDOW window AS (ORDER BY v) ORDER BY v
… 你会得到:
+---+------------+------+------------+ | V | ROW_NUMBER | RANK | DENSE_RANK | +---+------------+------+------------+ | a | 1 | 1 | 1 | | a | 2 | 1 | 1 | | a | 3 | 1 | 1 | | b | 4 | 4 | 2 | | c | 5 | 5 | 3 | | c | 6 | 5 | 3 | | d | 7 | 7 | 4 | | e | 8 | 8 | 5 | +---+------------+------+------------+
在添加字段时使用DISTINCT
会导致问题,还会掩盖select中的问题。 使用GROUP BY
作为替代,如下所示:
SELECT id ,ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS RowNum FROM table where fid = 64 group by id
然后,您可以添加其他有趣的信息,像这样:
,count(*) as thecount
要么
,max(description) as description
感觉如何?
;WITH DistinctVals AS ( SELECT distinct id FROM table where fid = 64 ) SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM DistinctVals
SQL小提琴演示
你也可以试试
SELECT distinct id, DENSE_RANK() OVER (ORDER BY id) AS RowNum FROM @mytable where fid = 64
SQL小提琴演示
尝试这个:
;WITH CTE AS ( SELECT DISTINCT id FROM table WHERE fid = 64 ) SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM cte WHERE fid = 64
尝试这个
SELECT distinct id FROM (SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS RowNum FROM table WHERE fid = 64) t
或者使用RANK()
而不是行号并selectloggingDISTINCT rank
SELECT id FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS RowNum FROM table WHERE fid = 64) t WHERE t.RowNum=1
这也会返回不同的ID