0

How can I achieve this table?

enter image description here

Here are the tables

enter image description here

enter image description here

And here is the query that I have come-up but I'm still having some error

SELECT DISTINCT Table1.ID as T1_ID,
       (SELECT *
          from Table1.Location
         where Table1.ID = Table1.ID) as T1_Location,
       Table2.Location as T2_Location
  FROM Table1
 INNER JOIN Table2 ON Table1.ID = Table2 = ID 
5
  • 1
    Removed conflicting product tags, pls add the one back that you are actually using! Commented Jul 21, 2021 at 17:43
  • What type of data do you expect for Table1.location? 3 strings concatenated? An array like structure (if the dB supports it)? Commented Jul 21, 2021 at 17:48
  • 1
    What is your DBMS product? Oracle? Sql Server? Mysql? Commented Jul 21, 2021 at 17:54
  • 1
    This can be achieved (at least in MySQL) with GROUP BY, but it will result in the second column being a concatenated string with the locations from Table1. Commented Jul 21, 2021 at 18:03
  • this is on oracle Commented Jul 21, 2021 at 23:56

3 Answers 3

2

I tested in SQL Flow with Oracle, it seems works

SELECT table1.ID, GROUP_CONCAT(table1.Location), table2.Location FROM table2 INNER JOIN table1 ON table2.ID = table1.ID GROUP BY table1.ID

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming a concatenated string is okay for the Table1.Location output, you can do something like:

SELECT t1.id, CONCAT(t1.location, '-'), t2.location
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
GROUP BY t1.id, t2.location

Syntax may vary slightly depending on your RDBMS

Comments

-1

You need an additional column to uniquely identify records on Table1, otherwise you won't have control over which record is matched, or you can sort alphabetical or maybe use ROW_NUMBER(). If you don't care which record is selected from table 1, the query bellow will do.

WITH tmpT1 AS (SELECT DISTINCT ID, Location FROM Table1)  
SELECT t1.ID AS T1_ID, t1.location AS T1_Location, t2.Location AS  T2_Location FROM Table2 AS t2 JOIN tmpT1 AS T1
ON t2.ID = T1.ID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.