Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

sql - How to join 2 different table which does not have any link

How to join 2 different table which does not have any link

Table 1 - Stock on hand (has all the item of the store)

Table 2 - Sales

Now I need data from Table 1 and Table 2 in one file Table 1

ItemCode SOH_Quantity
aaaaaaaa 10
bbbbbbbb 12
cccccccc 20

Table 2

ItemCode Quantity InvoiceNo Date
aaaaaaaa 1        001       25/01/2021

I need below output

ItemCode Quantity Type
aaaaaaaa 10        I
bbbbbbbb 12        I 
cccccccc 20        I
aaaaaaaa 1         S

please help me to fetch the expected output

question from:https://stackoverflow.com/questions/65886165/how-to-join-2-different-table-which-does-not-have-any-link

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you described is not a "join", it is a "union".

Use UNION ALL.

SELECT
    ItemCode
    ,SOH_Quantity AS Quantity
    ,'I' AS Type
FROM Table1

UNION ALL

SELECT
    ItemCode
    ,Quantity
    ,'S' AS Type
FROM Table2
;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...