我有两个查询,它从同一个表中选择一些数据
query1:
select rownum rn , error_data_log
from ext_tab_log
where error_data_log like'error%'
ORDER BY rn, error_data_log ;
结果:
+----+--------------------------+
| RN | error_data_log |
+----+--------------------------+
| 1 | error processing column |
+----+--------------------------+
查询2:
select rownum rn , error_data_log
from ext_tab_log
where error_data_log like 'KUP-04101%'
ORDER BY rn, error_data_log ;
结果:
+----+----------------------------------------------+
| RN | error_data_log |
+----+----------------------------------------------+
| 1 | KUP-04101: record 1 rejected in file abc.txt |
| 2 | KUP-04101: record 8 rejected in file abc.txt |
| 3 | KUP-04101: record 9 rejected in file abc.txt |
+----+----------------------------------------------+
我们如何编写SQL查询以获得以下结果:
+----+----------------------------------------------+
| RN | error_data_log |
+----+----------------------------------------------+
| 1 | error processing column |
| 2 | KUP-04101: record 8 rejected in file abc.txt |
+----+----------------------------------------------+
答案 0 :(得分:1)
如果我理解正确,你可以尝试这样的事情:
select rownum rn , error_data_log
from
select rownum rn , error_data_log, 1 As QueryNum
from ext_tab_log
where error_data_log like'error%'
UNION ALL
select rownum rn , error_data_log, 2 QueryNum
from ext_tab_log
where error_data_log like 'KUP-04101%'
group by rn , error_data_log, QueryNum
having rn > 1 and rn < max(rn)
) UnionSelect
ORDER BY QueryNum, rn, error_data_log ;
注意:直接写在这里的sql,我自己没试过。
答案 1 :(得分:1)
WITH KUP-04101 AS
(SELECT error_data_log
, CASE
WHEN LEAD(error_data_log) OVER (ORDER BY error_data_log) IS NULL
THEN 'Last'
WHEN LAG(error_data_log) OVER (ORDER BY error_data_log) IS NULL
THEN 'First'
END first_and_last
FROM ext_tab_log
WHERE error_data_log LIKE 'KUP-04101%')
SELECT error_data_log
FROM KUP-04101
WHERE first_and_last NOT IN ('First','Last')
UNION
SELECT error_data_log
FROM ext_tab_log
WHERE error_data_log LIKE 'error%'
ORDER BY error_data_log ;