Joining with a distinct column - sql

How do I join a table with a distinct value in a SQL view?
My code looks like below
Select a, b, c, d from TableA inner join TableB on TableA.account = TableB.account
I want TableB.account to have distinct values when I join the table.
The selected fields(a,b,c,d) do not have to be distinct values.

SELECT distinct_foo.thing
, bar.other_things
FROM (
SELECT DISTINCT
thing
FROM foo
) AS distinct_foo
INNER
JOIN bar
ON bar.thing = distinct_foo.thing
;

Related

Hive join tables and keep only 1 column

I have below table join and noticed that Hive keeps two copies of the pk column - one from table b and one from table c. Is there a way to keep only 1 of those columns?
I can always replace select * with exact select column1, column2 etc but that wont be too efficient
with a as (
select
*
from table1 b left join table2 c
on b.pk = c.pk
)
select
*
from a;
;
#update 1
is it possible to alias many columns?
for example the below line works
select b.pk as duplicate_pk
but is there a way to do something like
select b.* as table2 to add text table2 before all the columns of the table b?
Not sure if you already tried this but you can choose what to select using either
b.* to select cols of only table1
c.* to select cols of only table2
Example:
with a as (
select
b.*
from table1 b left join table2 c
on b.pk = c.pk
)
select
*
from a;

Multiple tables joined to a table via single column

I am trying to create query, on below scenario.
with my skills I am able to join Table A,A1,B and A,A1,C and A,A1,D individually and union them.
Is there any better way to achieve same. I am using Oracle as Database.
It all depends on what they mean and if you need to know the columns the values are from.
This would get all the columns and you would have NULL values from the non-matching B, C, D tables:
SELECT *
FROM a1
INNER JOIN a ON a1.aid = a.id
LEFT OUTER JOIN b ON a.extid = b.extid
LEFT OUTER JOIN c ON a.extid = c.extid
LEFT OUTER JOIN d ON a.extid = d.extid
Or, this would get only the relevant values and give you the type they belong to in fewer columns:
SELECT *
FROM a1
INNER JOIN a ON a1.aid = a.id
INNER JOIN (
SELECT extid, 'B' AS type, pqr_col AS col1, qrs_col AS col2 FROM b
UNION ALL
SELECT extid, 'C', abc_col, bcd_col FROM c
UNION ALL
SELECT extid, 'D', xyz_col, yza_col FROM d
) bcd
ON a.extid = bcd.extid
Union was my first thought when I read your question. Though, for simplicity, you could first create a view and then join it to other table(s):
create view v_ext as
select b.extid, b.pqr_col, b.qrs_col from b
union all
select c.extid, c.abc_col, c.bcd_col from c
union all
select d.extid, d.xyz_col, d.yza_col from d;
select *
from a join a1 on a.id = a1.aid
join v_ext v on v.extid = a.extid;
you can try the query with 'with' clause. Something like below, I havent tested it though
with union_output as
( select b.extid, b.pqr_col, b.qrs_col from b
union
select c.extid, c.abc_col, c.bcd_col from c
union
select d.extid, d.xyz_col, d.yza_col from d)
select *
from a join a1 on a.id = a1.aid
join union_output uo on uo.extid = a.extid;
Select *from tableA A
Inner join tableA1 A1 on A1.A1ID=A.AID
Inner join tableB b on b.ExtID=A.ExtID
Inner join tableC c on c.ExtID=A.ExtID
Inner join tableD d on d.ExtID=A.ExtID

How to join two tables on multiple columns using OR condition in bigquery SQL

Lets say I have two tables.
First table shown below:
tableA
Second table
tableB
Now I want to write a query That will join the two tables above on either name OR email OR phone.
Something like:
SELECT * FROM tableA
LEFT JOIN tableB
ON
(tableA.name_A = tableB.name_B OR tableA.email_A = tableB.email_B OR tableA.phone_A = tableB.phone_B)
And it should produce a table something like this
If you notice,
John matches rows between tableA and tableB on name.
Ally/allie matches rows between tableA and tableB on email.
Sam/Samual matches rows between tableA and tableB on phone
When I try to do this same query though I receive an
error that says LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
I am using BigQuery.
Please help, cheers
Try INNER JOIN
SELECT * FROM tableA
INNER JOIN tableB
ON
(tableA.name_A = tableB.name_B OR tableA.email_A = tableB.email_B OR tableA.phone_A = tableB.phone_B)
or CROSS JOIN:
SELECT * FROM tableA
CROSS JOIN tableB
WHERE
tableA.name_A = tableB.name_B
OR tableA.email_A = tableB.email_B
OR tableA.phone_A = tableB.phone_B
or UNION DISTINCT:
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.name_A = tableB.name_B
UNION DISTINCT
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.email_A = tableB.email_B
UNION DISTINCT
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.phone_A = tableB.phone_B
could you try by using parenthesis
SELECT * FROM tableA
LEFT JOIN tableB
ON
(tableA.name_A = tableB.name_B) OR
(tableA.email_A = tableB.email_B) OR
(tableA.phone_A = tableB.phone_B)

Main query results in Subquery

How can we use reference of main query result set as a source table in subquery
Table A, Table C
Select
(Select * From a)
From
(Select tabA.*
From A tabA
Join C tabC
On tabA.id = tabC.id) as a
I got invalid object a error here
Presumably, you want a common table expression (CTE):
with a as (
select tabA.*
from A tabA Join
C tabC
on tabA.id = tabC.id
)
Select (Select * from a)
From a;
That said, your query makes no sense. The scalar subquery is probably going to be returning an error, either because of the number of rows or number of columns.
if you using sqlserver than modified your query based on below query.
select * from
(select A.* from TableA A inner join TableB B on A.EmployeeID = B.EmployeeID ) a

LEFT JOIN - How to join tables and include extra row even if you have right match

I have two tables
Table A
-------
ID
ProductName
Table B
-------
ID
ProductID
Size
I want to join these two tables
SELECT * FROM
(SELECT * FROM A)
LEFT JOIN
(SELECT * FROM B)
ON A.ID = B.ProductID
This is easy, I will get all rows from A multiplied by rows matched in B, and NULL fields if there is no match.
But here comes the tricky question, how can I get all rows from A with NULL fields for table B, even if there is a match, so I get an extra line with NULL values plus all the matches?
SELECT A.*
, B3.ID
, B3.ProductID
, B3.Size
FROM A
LEFT JOIN
(
SELECT ProductID as MatchID
, ID
, ProductID
, Size
FROM B
UNION ALL
SELECT ID
, null
, null
, null
FROM A A2
) B3
ON A.ID = B3.MatchID
Live example at SQL Fiddle.
Instead of using UNION ALL in a subquery as suggested by others, you could also (and I would) use UNION ALL at the outer level, which keeps the query simpler:
SELECT A.ID, A.ProductName, B.ID, B.Size
FROM A
INNER JOIN B
ON B.ProductID = A.ID
UNION ALL
SELECT A.ID, A.ProductName, NULL, NULL
FROM A
Since every join is going to be successful, we can switch to a full/inner join:
SELECT
*
FROM
A
INNER JOIN
(SELECT ID,ProductID,Size FROM B
UNION ALL
SELECT NULL,ID,NULL FROM A) B
ON
A.ID = B.ProductID
Now would be a very good time to switch to naming columns explicitly, rather than using SELECT *
Or, if, as per #Andomar's comment, you need all of the B columns to be NULL:
SELECT
A.ID,A.ProductName,
B.ID,B.ProductID,B.Size
FROM
A
INNER JOIN
(SELECT ID,ProductID,Size,ProductID as MatchID FROM B
UNION ALL
SELECT NULL,NULL,NULL,ID FROM A) B
ON
A.ID = B.MatchID