I have two tables I want to compare data from. The query of the first table is below and it gives me 12 rows.
***** 1st Query ************
SELECT distinct a.pnr AS PNR, a.station_code as Station,a.date as Date,
sum(a.allamount) as TsrAmount
from daily_posting a
Where a.date between '2024-06-19' and '2024-06-19' and a.station_code in ('ABVS','BNIA','ABVH')
group by a.date,a.station_code,a.pnr
The second query which is similar gives me 17 rows
******* 2nd Query *********
SELECT distinct b.pnr_NO AS PNR, b.sR_code as Station,b.date_OF_ACTION as Date,
ABS(
sum(b.LOC_TAX_AMOUNT + b.LOC_PAX_FARE + b.LOC_SURCHARGE_AMOUNT + b.LOC_SERVICE_FEE_AMOUNT ))
as CRAmount
from ticketsalesdetails b
Where b.date_of_action between '2024-06-19' and '2024-06-19' and b.sr_code in ('ABVS','BNIA','ABVH')
group by b.date_of_action,b.sr_code,b.pnr_no
My Question is I want a full outer join to combine this two tables query and have the Amount column side by side for comaprison purpose. Mysql handles full outer join by doing LEFT JOIN and Use UNION to combine it with RIGHT join. I used the below query to perform my full outer join but the results are not right. It should give me 17 rows. but it is currently giving me 12 rows and the value of the amount is not correct
****** FULL OUTER JOIN *****
SELECT distinct a.pnr AS PNR, a.station_code as Station,a.date as Date,
sum(a.allamount) as TsrAmount,
ABS(sum(b.LOC_TAX_AMOUNT + b.LOC_PAX_FARE + b.LOC_SURCHARGE_AMOUNT + b.LOC_SERVICE_FEE_AMOUNT )) as CRAmount
from daily_posting a LEFT JOIN ticketsalesdetails b
ON (a.pnr,a.DATE,a.station) = (b.PNR_no,b.date_of_action ,b.sr_code)
Where a.date between '2024-06-19' and '2024-06-19' and a.station_code in ('ABVS','BNIA','ABVH')
group by a.date,a.station_code,a.pnr
UNION
SELECT distinct a.pnr AS PNR, a.station_code as Station,a.DATE as Date,
sum(a.allamount) as TsrAmount, -1* sum(b.LOC_TAX_AMOUNT + b.LOC_PAX_FARE + b.LOC_SURCHARGE_AMOUNT + b.LOC_SERVICE_FEE_AMOUNT ) as CRAmount
from daily_posting a RIGHT JOIN ticketsalesdetails b
ON (a.pnr,a.DATE,a.station) = (b.PNR_no,b.date_of_action ,b.sr_code)
Where a.date between '2024-06-19' and '2024-06-19' and a.station_code in ('ABVS','BNIA','ABVH')
group by a.date,a.station_code,a.pnr
I did a LEFT JOIN AND RIGHT JOIN AND COMBINED BOTH QUERIES WITH UNION BUT THE OUTPUT IS NOT CORRECT
2
Answers
As there are no sample data in your question, so, I tried to solve this issue logically.
Create two different view tables, A, B from daily_posting and ticketsalesdetails with same column name.
Then use
UNION of INNER JOIN (A∩B) and Difference of two table (A-B, B-A)
, according to VENN Diagram of OUTER JOINSo, you query should be:
Emulate FULL OUTER JOIN by the next way:
The query assumes that
PNR
,Station
andDate
are not NULL.If these columns may contain NULL then use