3

我的表值:

COLUMN1  COLUMN2 COLUMN3
WF1      Email   1640
WF1      Email   1641
WF1      Email   N/A
WF3      Email   N/A

预期结果:

COLUMN1  COLUMN2 COLUMN3
WF1      Email   1640
WF3      Email   N/A

我需要检索 column2 = 'Email' 的所有记录,如果 column1 包含重复值,我必须选择 column3 <> 'N/A' 的记录。

我阅读了有关分区的教程,但仍然不确定如何获得结果。

任何帮助表示赞赏。

CREATE TABLE TABLE1 
    (

     COLUMN1 varchar2(20), 
     COLUMN2 varchar2(20), 
     COLUMN3 varchar2(20) 
    );

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', '1640');

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', '1641');

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', 'N/A');

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF3', 'Email', 'N/A');
4

3 回答 3

3

尝试这样的事情:

SELECT column1, column2, column3
from(
SELECT column1, column2, column3,
row_number() over (partition BY column1, column2 ORDER BY CASE WHEN column3 = 'N/A' THEN 999999999 ELSE to_number(column3) END ) rn
FROM table1)
WHERE rn = 1

这是一个sqlfiddle

于 2013-04-12T09:35:28.687 回答
0

如果 column1 在结果中不是唯一的:

select column1, column3 from table1 t1 
where column3 != 'N/A' or 
column3 = 'N/A' and not exists
(select * from table1 t2 
where t2.column1 = t1.column1 and t2.column3 != 'N/A')

如果 column1 在结果中必须是唯一的:

select column1, column3 from table1 t1 
where column3 != 'N/A' and not exists
(select * from table1 t2 
where t2.column1 = t1.column1 and t2.column3 != 'N/A' and 
to_number(t2.column3, '9999') > to_number(t1.column3, '9999')
) union 
select column1, column3 from table1 t1 
where column3 = 'N/A' and not exists 
(select * from table1 t2 where t2.column1 = t1.column1 and t2.column3 != 'N/A')

请注意,我们使用 union 而不是 union all。

于 2013-04-15T12:44:21.107 回答
0
select column1,column2,max(case when column3 = 'N/A' then '0' else column3 end) column3
from table1
group by column1,column2;
于 2013-04-12T09:44:01.273 回答