我有一套SQL存储过程来使用分区来获得百分位数。通过执行以下分区,我能够正确获取百分位数据。但是我的问题是每行都有重复。例如,对于每个DESC,当假设仅为1行时,存在多个重复。为什么会这样?
row_nums AS
(
SELECT DATE, DESC, NUM, ROW_NUMBER() OVER (PARTITION BY DATE, DESC ORDER BY NUM ASC) AS Row_Num
FROM ******
)
SELECT .................
这是我目前得到的输出:(返回的行有重复行 - 请参阅第6行到第8行) http://i.stack.imgur.com/foe7g.png[ ^]
这是我想要实现的输出:http://i.stack.imgur.com/GkrHP.png[ ^]
答案 0 :(得分:0)
您可以通过在FROM子句中再添加一个INNER查询来删除重复项,如下所示:
;WTIH row_nums AS
(
SELECT DATE, DESC, NUM, ROW_NUMBER() OVER (PARTITION BY DATE, DESC ORDER BY NUM ASC) AS Row_Num
FROM (
SELECT your required columns, COUNT(duplicated_rows_columnsname)
FROM ***
GROUP BY columnnames
HAVING COUNT(duplicated_rows_columnsname) = 1
)
)
SELECT .................
但是,您也可以使用INNER中的DISTINCT子句删除重复行。查询。