Ihre Beispielausgabe die Tabellendaten nicht wirklich überein ; wenn wir den 100
Wert einfach ignorieren kann dann wollen Sie wahrscheinlich so etwas wie diese (was Tabellendaten in CTEs):
with table_a (AMT, ID, CRD, STN, TYPE) as (
select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 11500, 7132323, 992211, 556611, 0200 from dual
union all select 10000, 7132323, 992211, 556611, 0200 from dual
union all select 35200, 7178866, 223344, 989898, 0200 from dual
),
table_b (AMT, ID, CRD, STN, TYPE) as (
select 67000, 7123344, 556677, 442233, 0220 from dual
union all select 20000, 7132323, 992211, 556611, 0220 from dual
union all select 67100, 7199933, 667733, 343433, 0220 from dual
)
select a.id, a.crd, a.stn, a.sum_amt, b.sum_amt, a.sum_amt - b.sum_amt as diff
from (
select id, crd, stn, type, sum(amt) as sum_amt
from table_a
group by id, crd, stn, type
) a
inner join (
select id, crd, stn, type, sum(amt) as sum_amt
from table_b
group by id, crd, stn, type
) b
on b.id = a.id and b.crd = a.crd and b.stn = a.stn and b.type != a.type
order by a.id, a.crd, a.stn;
ID CRD STN SUM_AMT SUM_AMT DIFF
---------- ---------- ---------- ---------- ---------- ----------
7123344 556677 442233 66000 67000 -1000
7132323 992211 556611 21500 20000 1500
Die Unterabfragen (Inline-Ansichten) erzeugen, um die Summen für jede ID/CRD/STN/TYPE und diese werden dann zusammengefügt, so dass die äquivalenten Summen abgezogen werden können. Trotzdem hat Ihr Ergebnis beide positive Zahlen; wenn es das ist, was Sie wollen, dann können Sie nur ändern, es
abs(a.sum_amt - b.sum_amt) as diff
zu tun, oder Sie können die Subtraktion in die andere Richtung gehen wollen, so dass Sie +1000 und -1500 bekommen.
Es ist möglich, dass Sie auch den Unterschied für Kombinationen wollen, dass nur in exist in Tabelle A:
select a.id, a.crd, a.stn, a.sum_amt, b.sum_amt,
coalesce(a.sum_amt, 0) - coalesce(b.sum_amt, 0) as diff
from (
select id, crd, stn, type, sum(amt) as sum_amt
from table_a
group by id, crd, stn, type
) a
left outer join (
select id, crd, stn, type, sum(amt) as sum_amt
from table_b
group by id, crd, stn, type
) b
on b.id = a.id and b.crd = a.crd and b.stn = a.stn and b.type != a.type
order by a.id, a.crd, a.stn;
ID CRD STN SUM_AMT SUM_AMT DIFF
---------- ---------- ---------- ---------- ---------- ----------
7123344 556677 442233 66000 67000 -1000
7132323 992211 556611 21500 20000 1500
7178866 223344 989898 35200 35200
oder für Kombinationen, die entweder in der Tabelle A oder Tabelle B oder beide angezeigt:
select coalesce(a.id, b.id) as id, coalesce(a.crd, b.crd) as crd,
coalesce(a.stn, b.stn) as stn, a.sum_amt, b.sum_amt,
coalesce(a.sum_amt, 0) - coalesce(b.sum_amt, 0) as diff
from (
select id, crd, stn, type, sum(amt) as sum_amt
from table_a
group by id, crd, stn, type
) a
full outer join (
select id, crd, stn, type, sum(amt) as sum_amt
from table_b
group by id, crd, stn, type
) b
on b.id = a.id and b.crd = a.crd and b.stn = a.stn and b.type != a.type
order by coalesce(a.id, b.id), coalesce(a.crd, b.crd), coalesce(a.stn, b.stn);
ID CRD STN SUM_AMT SUM_AMT DIFF
---------- ---------- ---------- ---------- ---------- ----------
7123344 556677 442233 66000 67000 -1000
7132323 992211 556611 21500 20000 1500
7178866 223344 989898 35200 35200
7199933 667733 343433 67100 -67100
Diese verwenden linke oder vollständige äußere Verknüpfungen und fügen coalesce()
Aufrufe hinzu, um mit Daten umzugehen, die nicht in der einen oder in der anderen Inline-Ansicht vorhanden sind.
Mit Ihrer geänderten Daten und die Umkehrung der Berechnung des Vorzeichens Sie wollen zu bekommen, wird dies das gewünschte Ergebnis zu erwarten:
with table_a (AMT, ID, CRD, STN, TYPE) as (
select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 22000, 7123344, 556677, 442233, 0200 from dual
union all select 11500, 7132323, 992211, 556611, 0200 from dual
union all select 10000, 7132323, 992211, 556611, 0200 from dual
union all select 35200, 7199933, 223344, 989898, 0200 from dual
),
table_b (AMT, ID, CRD, STN, TYPE) as (
select 67000, 7123344, 556677, 442233, 0220 from dual
union all select 20000, 7132323, 992211, 556611, 0220 from dual
union all select 35300, 7199933, 223344, 989898, 0220 from dual
)
select a.id, a.crd, a.stn, a.sum_amt, b.sum_amt, b.sum_amt - a.sum_amt as diff
from (
select id, crd, stn, type, sum(amt) as sum_amt
from table_a
group by id, crd, stn, type
) a
inner join (
select id, crd, stn, type, sum(amt) as sum_amt
from table_b
group by id, crd, stn, type
) b
on b.id = a.id and b.crd = a.crd and b.stn = a.stn and b.type != a.type
order by a.id, a.crd, a.stn;
ID CRD STN SUM_AMT SUM_AMT DIFF
---------- ---------- ---------- ---------- ---------- ----------
7123344 556677 442233 66000 67000 1000
7132323 992211 556611 21500 20000 -1500
7199933 223344 989898 35200 35300 100
Es wird eine ziemlich grundlegende Abfrage tun, eine Verknüpfung und Gruppe zu dies erreichen. Hast du viel SQL geschrieben? – slambeth
Sie sollten wirklich zeigen, was Sie versucht haben und warum es nicht funktioniert. Warum ist der dritte Unterschied 100 - welche ID soll das sein? Und brauchen Sie einen Unterschied für die Daten, die nur in einer der (beiden) Tabellen liegen? Und der absolute Wert der Unterschiede (in Ihrem Beispiel ist einer der ersten beiden positiv, der andere negativ)? –
Ich habe es korrigiert. Grüße, –