ORACLE SQL How to implement a UNION clause but maintain the order of each SELECT -
using union
clause in 2 select
statements how can guarantee order of execution? example:
select a.poclcdde, a.poclnpol, a.poclcdcl dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , poclcdce = 0 , pocltpsg = '01' union select a.poclcdde, a.poclnpol, a.poclcdcl dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , pocltpsg = '02' poclcdde poclnpol poclcdcl ---------- ---------- ---------- 100 1000001 202153 100 5001021 216450 100 9000386 8078 100 9900633 250056 100 9900634 250056 100 9901720 562223 100 9901763 562223 200 1000001 202153 200 5001021 216450
in case, how can guarantee first records query1 , rest query2. use poclcdcl
column (or virtual column) , order that, in case need distinct
rows.
select * (select a.poclcdde, a.poclnpol, a.poclcdcl, 1 type dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , poclcdce = 0 , pocltpsg = '01' union select a.poclcdde, a.poclnpol, a.poclcdcl, 2 type dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , pocltpsg = '02') order type poclcdde poclnpol poclcdcl type ---------- ---------- ---------- ---------- 200 1000001 202153 1 100 1000001 202153 1 100 9000386 8078 1 100 9900634 250056 2 100 9901720 562223 2 100 9901763 562223 2 200 5001021 216450 2 100 9000386 8078 2 100 5001021 216450 2 100 9900633 250056 2
i need interact each row order: first query prevails. thanks
you can use union all
type
"virtual" column right order, , use filter on returned value of row_number
analytic function remove duplicates while prioritizing first query's rows:
with cte ( select a.poclcdde, a.poclnpol, a.poclcdcl, 1 type dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , poclcdce = 0 , pocltpsg = '01' union select a.poclcdde, a.poclnpol, a.poclcdcl, 2 type dtpocl a.poclcdcl in (216450, 562223, 250056, 202153, 8078) , pocltpsg = '02' ) select poclcdde, poclnpol, poclcdcl (select t.*, row_number() on ( partition t.poclcdde, t.poclnpol, t.poclcdcl order t.type) rn cte t) rn = 1 order type
Comments
Post a Comment