给出正确的Sql查询

时间:2012-09-12 09:12:26

标签: sql oracle10g db2

我有两个问题,

SELECT col1,col2,col3
  from scnd1
 where col2<>'' 
 group by col1,col2,col3 
 order by col1

SELECT ROWNUMBER() OVER (PARTITION BY COL1) AS RN FROM SCND1)AS A
 WHERE RN > 1

对于同一个表,我需要单个查询来组合这两个  即。首先,我想对表进行排序以及删除NULL,然后通过第二次查询删除重复的行。

1 个答案:

答案 0 :(得分:0)

尝试子查询,按照以下方式:

SELECT 
    main.col1, 
    main.col2,
    main.col3 
FROM scnd1 main
JOIN (SELECT 
         col1,
         col2,
         col3, 
         ROW_NUMBER() OVER (PARTITION BY(col1) ORDER BY col1) AS RN
      FROM SCND1
      WHERE col2<>''
) sub
ON sub.Col1 = main.Col1 AND sub.Col2 = main.col2 AND sub.Col3 = main.col3
WHERE RN > 1
GROUP BY main.col1,main.col2,main.col3 
ORDER BY main.col1