Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 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 Reply

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
;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...