我在Oracle表中有以下数据:
Date Amount Flag
12/09/2018 0:00 191 1
12/09/2018 0:00 190 0
13/09/2018 0:00 187 0
14/09/2018 0:00 200 1
14/09/2018 0:00 201 0
我想检索所有带有0标志的记录,并且如果有重复的行带有1标志,则应该优先使用。因此,上表的最终结果应如下所示:
Date Amount Flag
12/09/2018 0:00 191 1
13/09/2018 0:00 187 0
14/09/2018 0:00 200 1
答案 0 :(得分:1)
您可以尝试这样:
select * from <<table>> outer where outer.flag= 1
and exists (select * from <<table>> inner where inner.flag = 0 and inner.date=outer.date)
UNION
select * from <<table>> outer where outer.flag= 0 and
not exists (select * from <<table>> inner where inner.flag = 1 and inner.date=outer.date) ;
答案 1 :(得分:1)
我会尝试:
SELECT date, amount, flag
FROM tableName
HAVING flag = MAX(flag)
GROUP BY date, amount,flag
答案 2 :(得分:1)
您可以先尝试检索标志为1的所有记录,然后查找标志为0的所有未出现在第一组检索到的记录中的记录:
SELECT * FROM flags WHERE flag = 1
UNION
SELECT * FROM flags WHERE flag = 0
AND flagdate NOT IN (SELECT flagdate FROM flags WHERE flag = 1);
这适用于示例数据。
SQLFiddle:http://sqlfiddle.com/#!9/69dc02/2