删除查询中的重复行

时间:2013-10-09 06:20:38

标签: asp.net sql

SELECT ROW_NUMBER() 
         OVER( 
           ORDER BY QUESID DESC)                     AS RowNum, 
       QUESID, 
       QUESCATEGORY, 
       QUESTION, 
       ID                                            OptionID, 
       OPTION_NAMES, 
       CONVERT(VARCHAR(9), ISNULL(RA.REVIEWERID, ''))ReviewerID, 
       CONVERT(VARCHAR(9), ISNULL(ANSWER, ''))       Answer 
FROM   EMP_TB_APP_QUESTIONMASTER QM 
       INNER JOIN EMP_TB_EOB_QUESTIONBANK_OBJECTIVEOPTIONS QO 
               ON QM.QUESID = QO.QUESTIONID 
       INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_FROM_ANSWERS RA 
               ON QO.ID = RA.QUESTIONID 
       INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_REVIEWER R 
               ON R.REVIEWERID = RA.REVIEWERID 

我需要删除所有重复的行(无需比较RowNum列)

2 个答案:

答案 0 :(得分:0)

使用distinct删除重复的行

SELECT distinct ROW_NUMBER() 
         OVER( 
           ORDER BY QUESID DESC)                     AS RowNum, 
       QUESID, 
       QUESCATEGORY, 
       QUESTION, 
       ID                                            OptionID, 
       OPTION_NAMES, 
       CONVERT(VARCHAR(9), ISNULL(RA.REVIEWERID, ''))ReviewerID, 
       CONVERT(VARCHAR(9), ISNULL(ANSWER, ''))       Answer 
FROM   EMP_TB_APP_QUESTIONMASTER QM 
       INNER JOIN EMP_TB_EOB_QUESTIONBANK_OBJECTIVEOPTIONS QO 
               ON QM.QUESID = QO.QUESTIONID 
       INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_FROM_ANSWERS RA 
               ON QO.ID = RA.QUESTIONID 
       INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_REVIEWER R 
               ON R.REVIEWERID = RA.REVIEWERID 

答案 1 :(得分:0)

在将ROW_NUMBER添加到查询之前尝试使用distinct。

这样的事情:

SELECT *, 
       ROW_NUMBER() 
         OVER( 
           ORDER BY QUESID DESC) AS RowNum 
FROM   (SELECT DISTINCT QUESID, 
                        QUESCATEGORY, 
                        QUESTION, 
                        ID                                            OptionID, 
                        OPTION_NAMES, 
                        CONVERT(VARCHAR(9), ISNULL(RA.REVIEWERID, ''))ReviewerID 
                        , 
                        CONVERT(VARCHAR(9), ISNULL( 
                        ANSWER, ''))       Answer 
        FROM   EMP_TB_APP_QUESTIONMASTER QM 
               INNER JOIN EMP_TB_EOB_QUESTIONBANK_OBJECTIVEOPTIONS QO 
                       ON QM.QUESID = QO.QUESTIONID 
               INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_FROM_ANSWERS RA 
                       ON QO.ID = RA.QUESTIONID 
               INNER JOIN EMP_TB_TRAN_PROBATION_REVIEW_REVIEWER R 
                       ON R.REVIEWERID = RA.REVIEWERID)T