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
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 |
CodePudding user response:
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';