2016-08-11 81 views
0

我已經寫了一個查詢,它返回下列數據。SQL中RowNum的有條件選擇

ID EmpFirstName EmpLastName RowNum 
1  X     Y  1 
2  A     B  1 
3  A     B  2 

現在我想要RowNum> 1的所有記錄。例如,在這種情況下,我需要輸出2和3記錄。

如果我把條件RowNum >1然後我只會得到第三條記錄,但我也想要2。

+0

如果第二條記錄有'RowNum' ='1',那麼爲什麼你要在你的結果集?這聽起來與我矛盾。 –

+0

其實@TimBiegeleisen如果設置有任何重複的記錄,那麼我需要完整的記錄信息。原始數據集中有很多字段,顯然重複將被刪除,但是要做出決定,兩個記錄都需要輸出。如果我選擇'RowNum = 2',那麼決定是否刪除第2條記錄或第1條記錄。 –

+0

我看到重複的第一行和第二行,但是它們的'RowNum'值仍然小於1.爲什麼包含它們? –

回答

1

假設你的查詢是這樣的:

select ID, EmpFirstName, EmpLastName, 
ROW_NUMBER() OVER (PARTITION BY EmpFirstName, EmpLastName ORDER BY ID) AS RowNum 
FROM aTable 

這是用來過濾掉任何重複值的經典查詢。

爲了有效地選擇所有重複值的記錄,我可以建議使用COUNT()窗口功能:

;with a as (
    select ID, EmpFirstName, EmpLastName, 
    ROW_NUMBER() OVER (PARTITION BY EmpFirstName, EmpLastName ORDER BY ID) AS RowNum, 
    COUNT(*) OVER (PARTITION BY EmpFirstName, EmpLastName) AS cnt 
    FROM aTable 
) 
SELECT * FROM a where cnt > 1 
ORDER BY EmpFirstName, EmpLastName 

爲了測試它使用此查詢:

drop table #tmp 
CREATE table #tmp (ID int , EmpFirstName varchar(10) , EmpLastName varchar(10)) 
go 
INSERT INTO #tmp VALUES 
    (1,'X','Y') 
    ,(2,'A','B') 
    ,(3,'A','B') 
    ,(4,'A','C') 
    ,(5,'B','C') 
    ,(6,'B','C') 


;with a as (
    select ID, EmpFirstName, EmpLastName, 
    ROW_NUMBER() OVER (PARTITION BY EmpFirstName, EmpLastName ORDER BY ID) AS RowNum, 
    COUNT(id) OVER (PARTITION BY EmpFirstName, EmpLastName) AS cnt 
    FROM #tmp 
) 
SELECT * FROM a where cnt > 1 
ORDER BY EmpFirstName, EmpLastName 

結果:

ID   EmpFirstName EmpLastName RowNum    cnt 
----------- ------------ ----------- -------------------- ----------- 
2   A   B   1     2 
3   A   B   2     2 
5   B   C   1     2 
6   B   C   2     2 
+0

我喜歡邏輯'COUNT(*)OVER(PARTITION BY EmpFirstName,EmpLastName)AS cnt',但它沒有工作..它在計數結束EmpFirstName而不考慮姓氏:( –

+0

適用於我:'drop table #tmp CREATE table #tmp(ID int,EmpFirstName varchar(10),EmpLastName varchar(10)) go INSERT INTO #tmp VALUE (1,'X','Y') ,(2,'A',' B') ,(3,'A','B') ,(4,'A','C') ,(5,'B','C') ,(6''B' ,'C') ; with a( select ID,EmpFirstName,EmpLastName, ROW_NUMBER()OVER(PARTITION BY EmpFirstName,Emp (*)OVER(PARTITION BY EmpFirstName,EmpLastName)AS cnt FROM #tmp ) )SELECT * FROM a where cnt> 1' – cha

+0

好吧,我想我覺得爲什麼它似乎你這是由EmpFirstName計數,這是因爲你的結果集是未排序的。將'ORDER BY EmpFirstName,EmpLastName'添加到查詢的末尾,您將得到正確的結果 – cha

0

我製作樣本數據並使用此查詢

CREATE table #tmp (ID int , EmpFirstName varchar(10) , EmpLastName varchar(10) ,RowNum int) 
INSERT INTO #tmp VALUES 
    (1,'X','Y',1) 
    ,(2,'A','B',1) 
    ,(3,'A','B',2) 

SELECT ID,EmpFirstName,EmpLastName,RowNum 
FROM (
    SELECT * 
     ,ROW_NUMBER() OVER (ORDER BY ID) AS [NEWrownum] 
    FROM #tmp 
) q 
WHERE q.NEWrownum > 1 
0

試試這個,

DECLARE @Result TABLE (ID INT, EmpFirstName VARCHAR(10), EmpLastName VARCHAR(10), RowNum INT) 

INSERT INTO @Result 
VALUES 
    (1, 'X', 'Y', 1) 
    ,(2, 'A', 'B', 1) 
    ,(3, 'A', 'B', 2) 

SELECT r1.* 
FROM @Result r1 
INNER JOIN (SELECT *  -- get duplicate records 
      FROM @Result 
      WHERE RowNum = 2     
      ) as r2 ON r1.EmpFirstName = r2.EmpFirstName 
        AND r1.EmpLastName = r2.EmpLastName