我的桌子值:
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');
答案 0 :(得分: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
答案 1 :(得分:0)
select column1,column2,max(case when column3 = 'N/A' then '0' else column3 end) column3
from table1
group by column1,column2;
答案 2 :(得分: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。