Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting multiple records from 2 tables in one row

I need to extract datas from a first table, contening the codes, and then encode said codes with a second table.

The first table looks like:

TABLE1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ID Payment type Country Business Sector
00001 11111111 11111111 11111111
00002 22222222 22222222 22222222
00003 33333333 33333333 33333333

The second table looks like

TABLE2

ID Description Type econde
11111111 Cash Pay
22222222 Bank Transfer Pay
33333333 Bank Check Pay
11111111 Italy Country
22222222 England Country
33333333 USA Country
11111111 First Business
22222222 Second Business
33333333 Third Business

The query I would need to extract is the following

ID Payment type Country Business Sector
00001 Cash Italy First
00002 Bank Transfer England Second
00003 Bank Check USA Third

If possible I would want to extract only 1 record selecting by the ID

ID Payment type Country Business Sector
00002 Bank Transfer England Second

>Solution :

We can try joining the first table to the second one, thrice:

SELECT
    t1.ID,
    t2a.Descritption AS "Payment type",
    t2b.Description AS Country,
    t2c.Description AS "Business Sector"
FROM TABLE1 t1
LEFT JOIN TABLE2 t2a
    ON t2a.ID = t1."Payment type" AND t2a."Type econde" = 'Pay'
LEFT JOIN TABLE2 t2b
    ON t2b.ID = t1.Country AND t2b."Type econde" = 'Country'
LEFT JOIN TABLE2 t2c
    ON t2c.ID = t1."Business Sector" AND t2c."Type econde" = 'Business';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading