Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

Left Outer Join on same table clarification

$
0
0

HI,

I have a table that gets populated from 3<sup>rd</sup> party system. We don’t have control over it. So, the table has master record (master) and children. Master type is 78 and children’s type is 64. So, it looks like this. In the 3<sup>rd</sup> party system, if Master transaction gets cancelled, it is recorded as type 178. If child is cancelled, then it is 164. Once the child is cancelled and created again using one process then newly created transaction will have 65 as type. Same thing with Master cancelled transaction also. It will be 79. So, to summarize:

Master:                                                                                                                                                                      

Brand New Transaction type = 78

Cancelled Transaction type = 178

Cancelled with creation transaction type = 79

Child:

Brand New Transaction type = 64

Cancelled Transaction type = 164

Cancelled with creation transaction type = 65

I don’t have to bother about master records. I need to focus on only children for my query.

ID

TxnID

Master

Type

TDate

Location

193075

211554

211543

64

20140805

ABC

193076

211555

211543

64

20140805

NBC

193077

211556

211543

64

20140805

ABC

193080

211559

211558

64

20140805

ABC

193081

211562

211561

64

20140805

ABC

193082

211565

211564

64

20140805

CBC

193083

211565

211564

164

20140805

CBC

193084

211566

211564

65

20140805

AZC

--droptable#Transactions

CREATETABLE#Transactions

(

IDint,

TxnIDint,

mstTicketint,

Typecodeint,

Tdatedatetime,

Locationvarchar(10)

)

select*from #Transactions

Insertinto#Transactions(ID,TxnID, mstTicket,Typecode,Tdate,Location)

Select 193075, 211554,211543,64,'2014-08-05','ABC'UNIONALL

Select 193076, 211555,211543, 64,'2014-08-05', 'NBC'UNIONALL

Select 193077, 211556, 211543, 64,'2014-08-05',                               'ABC'UNIONALL

Select 193080, 211559, 211558, 64,'2014-08-05',                               'ABC'UNIONALL

Select 193081, 211562, 211561, 64,'2014-08-05',                               'ABC'UNIONALL

Select 193082, 211565, 211564, 64,'2014-08-05',                               'CBC'UNIONALL

Select 193083, 211565, 211564, 164,'2014-08-05',                             'CBC'UNIONALL

Select 193084, 211566, 211564, 65,'2014-08-05',                               'AZC'

selectT.TxnID,T.TypeCode,T.Location,TL.TxnID

From#TransactionsT

    LeftOuterJOIN#TransactionsTLON TL.TxnID =T.TxnIDandTL.TypeCode= 164

selectT.TxnID,T.TypeCode,T.Location,TL.TxnID

From#TransactionsT

    LeftOuterJOIN#TransactionsTLON TL.TxnID =T.TxnIDandTL.TypeCode= 164

WhereT.typecodein(64, 65)

I need a clarification regarding left Outer Join. 

In the first left outer join query both 64 and 164 both have TL.TxnID populated. Why is that?. What I understand from

left outer join is that ‘Returns all the rows’ from left table and only matching data from right table.

Here, matching row from right table is 211565 and 164 record (id 193083). So, only it should have TxnID populated. But row 211565 and 64 has TL.txnID getting populated (ID 193082).

Why is it? Am I not understanding left out join properly?

Thanks,


Viewing all articles
Browse latest Browse all 12890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>