SQL Request for joining a table and printing its column twice in the result table - sql

There are two tables (A, B) in my database and I'm trying to join them and print the same column of table B twice in the result table. The problem is that I need to put in compliance with two different columns of table A, so to join these tables also twice. I tried to join them and put in the select expression required column two times, but in this case result table shows the same relation in every column.
Here is the result I want, where the id column of table B includes id_1 and id_2 from table A and the output names correspond to ids from table A.
Table A: (integer id_1, integer id_2)
Table B: (integer id, varchar name)
Result Table: (id_1, name, id_2, name)

You need inner join with tableB twice as follows:
select a.id_1, b1.name, a.id_2, b2.name
from tableA a join tableB b1 on a.id_1 = b1.id
join tableB b2 on a.id_2 = b2.id

Related

How do I SELECT only rows from table b that have all the same values for a given column per foreign key?

I have a table that just has ID's.
Another table that has ID plus a couple other columns.
One such column is [set], for reference.
I am trying to build a join query on only the ID's in table2 that have the same value for every row in column [set], not just ID'S that have a duplicate value in [set] plus another different value. So, each ID in table2 can have multiple rows.
table1
[id]
a1
a2
table2
[id]
[op]
[set]
a1
22
cut
a1
21
cut
a2
23
cut
a2
25
cut
a2
24
slice
In the given example, 'a2' would not fit the criteria because the values in column [set] are not all the same.
My query isn't working.
SELECT DISTINCT(A.ID)
FROM TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
GROUP BY A.ID, B.SET
HAVING COUNT(DISTINCT(B.SET)) =1
You're grouping by id and set, so by definition within each group, there will be just one value for set. Remove it from the group by clause and you should be OK. Also, not that since you're grouping by the id, you don't need a distinct on it:
SELECT A.ID -- Distinct removed. It's not wrong, just redundant
FROM TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
GROUP BY A.ID -- Grouping by b.set removed
HAVING COUNT(DISTINCT(B.SET)) = 1

SQL query to append values not contained in second table

I have table A and table B with different number of columns but both containing a column with IDs. Table A contains more complete list of IDs and table B contains some of the IDs from the table A.
I would like to return resulting table B with original information plus appended IDs that are missing in B but contained in A. For these appended rows, other columns should be blank while column with IDs in B should just contain missing ID values.
Simple solution UNION ALL, with NOT EXISTS:
select b.id, b.c1, ..., b.cn
from b
UNION ALL
select distinct a.id, null, ..., null -- should be same number of columns as in the above select
from a
where not exists (select 1 from b where b.id = a.id)
I think you described left join:
select *
from b left join
a
using (id)

Postgres SQL SELECT data from the table the entry exists with ID

I have the following scenario.I have 3 tables with the following structure.
TABLE A
-entry_id (PRIMARY KEY, INTEGER)
TABLE B
-entry_id (FOREIGN_KEY -> TABLE A)
-content (TEXT)
TABLE C
-entry_id (FOREIGN_KEY -> TABLE A)
-content (INTEGER)
I want to retrive the content cell value from either table B or table C. The value can be in just one of the table. So it is either table B or C witch have an entry with a given entry_id.
PS. Sorry if duplicate did not manage to find anything to match what i need.
If I correctly understand, you need something like:
select entry_id, content::text from TABLEB where entry_id = ?
union all
select entry_id, content::text from TABLEC where entry_id = ?
union all
select entry_id, content::text from TABLED where entry_id = ?
If it can only exist in one table at a time, use a union
select a1.entry_id, b2.content
from TableA a1
inner join TableB b2
on a1.entry_id = b2.entry_id
union -- This removes any duplicates. Use UNION ALL to show duplicates
select a1.entry_id, c3.content::text
from TableA a1
inner join TableC c3
on a1.entry_id = c3.entry_id

The Difference Inner Join Query

I'm just curious, if i have table a and table b.
I write query 1:
SELECT * FROM table a INNER JOIN table b ON table a.id = table b.id
I write query 2:
SELECT * FROM table b INNER JOIN table a ON table b.id = table a. id
What is the difference both of above query?
Thank you
When using INNER JOIN , there is no difference in resultset returned except in order of columns when SELECT * is used i.e. columns are not explicitly mentioned.
SELECT *
FROM table a
INNER JOIN table b
ON table a.id = table b.id
returns columns from tableA followed by columns from tableB
SELECT *
FROM table b
INNER JOIN table a
ON table b.id = table a. id
returns columns from tableB followed by columns from tableA
The second table matches data with the first one.
So it is better to put smaller table on the second place.

Return single row when 2 records in right table

I have two related sql server tables ... TableA and TableB.
***TableA - Columns***
TableA_ID INT
VALUE VARCHAR(100)
***TableB - Columns***
TableB_ID INT
TableA_ID INT
VALUE VARCHAR(100)
For every single record in TableA there are always 2 records in TableB. Therefore TableA has a one-to-many relationship with TableB.
How could I write a single sql statement to join these tables and return a single row for each row in TableA that includes:
a column for the VALUE column in the first related row in table B
a column for the VALUE column in the second related row in table B?
For exactly 2 related records, that's easy. Join table B twice:
SELECT
A.TableA_ID,
A.VALUE AS VALUE_A
B1.VALUE AS VALUE_B1
B2.VALUE AS VALUE_B2
FROM
TableA AS A
INNER JOIN TableB B1 ON B1.TableA_ID = A.TableA_ID
INNER JOIN TableB B2 ON B2.TableA_ID = A.TableA_ID
WHERE
B1.TableB_ID < B2.TableB_ID
If you have a column in table B that determines what is "first value" and what is "second value", this gets even easier (and it would work like this for N columns in table B, just add more joins):
SELECT
A.TableA_ID,
A.VALUE AS VALUE_A
B1.VALUE AS VALUE_B1
B2.VALUE AS VALUE_B2
FROM
TableA AS A
INNER JOIN TableB B1 ON B1.TableA_ID = A.TableA_ID AND B1.Type = '1'
INNER JOIN TableB B2 ON B2.TableA_ID = A.TableA_ID AND B2.Type = '2'
A composite index on TableB over (TableA_ID, Type) helps this join.