Select top 1 of table - sql

Trying to select top record of a outer joined table
if there are no records in table B then null will be there
if there are multiple records then just the first one should be selected.
I built this query but I get error
SELECT DISTINCT
A.Col1 , A.Col2, B.Col2, B.Col3
FROM
A LEFT OUTER JOIN (SELECT TOP 1 * FROM B WHERE B.Col1=A.Col1) A ON B.Col1=A.Col1
The multi-part identifier "B.Col1" could not be bound.
Anyone know how to resolve this?

If you want only one match, then use outer apply:
SELECT A.Col1 , A.Col2, B.Col2, B.Col3
FROM A OUTER APPLY
(SELECT TOP 1 *
FROM B
WHERE B.Col1 = A.Col1
) B;

Related

Left join with a predicate on the inner table in the ON clause

I have a query like this:
Query 1)
select A.col1, B.col2
from A
left join B on A.id= B.id and B.col3 = 'Hello';
I want to rewrite it to use a temp table for performance issue (I need the result the be exactly the same):
Query 2.1 and 2.2
Select B.id, B.col2
into #temp
from B
where B.col3 ='Hello';
select A.col1, t.col2
from A
left join #temp AS t on A.id= t.id;
But my result is not the same (the temp table version has some nulls in B.col2 where the first version does not have).
for me, both queries have the same result
in your first query, you have LEFT join, this means all the rows from table A. Try inner join instead.

Can you use an AND statement in a JOIN in SQL

Suppose you have two tables A and B and you are trying to write a JOIN query, is the following possible:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
Will this return a table of col1 from table A and col2 from table B where there is a match in the second column across the tables and the third column of table B is 'hello'?
I.e. it will only return rows that are matching in col2 and this is further reduced to the cases where col3 in table B is 'hello'?
Yes. You can use:
Below will Join the Records in B table (Col3='hello') with A:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
Below will Join all Records in B table with A, And performing where at Result of A and B:
SELECT A.col1, B.col1
FROM A JOIN B on A.col2 = B.col2
WHERE B.col3 = 'hello'
Both will give the same result when no other tables joined.
Yes you can.
You can specify any kind of boolean condition in the ON clause.
It is not mandatory that any column is involved in the condition so all of the following are valid:
SELECT A.col1, B.col1 FROM A JOIN B on 1=1
SELECT A.col1, B.col1 FROM A JOIN B on B.col3 = 'hello'
SELECT A.col1, B.col1 FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
SELECT A.col1, B.col1 FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = C.col3)
SELECT A.col1, B.col1 FROM A LEFT JOIN B on (C.col3 = 'bye')
But pay attention, if you limit the condition to only key fields the optimizer engine will improve the performances very much.
For an inner join, these two statements are equivalent:
SELECT A.col1, B.col1
FROM A JOIN
B
ON A.col2 = B.col2 AND B.col3 = 'hello';
and:
SELECT A.col1, B.col1
FROM A JOIN
B
ON A.col2 = B.col2
WHERE B.col3 = 'hello';
Both should have the same execution plans as well.
Some people prefer putting filtering conditions in the WHERE clause, so the query is more clear about "conditions between tables" versus "filters on the result set". I tend to agree with this sentiment, although I'm not dogmatic about it.
OUTER JOINs are different. For an outer join, it makes a big different where the conditions go. In that case, you generally do not have a choice, so you use ON or WHERE to get the logic that you want.

Hive lateral view explode with 2 table joins

Checking to see if this is possible in Hive:
Select a.col1,b.col1
from tableA a join tableB b on a.col1 = b.col1
lateral view explode(numcred) tableA as creds
where creds.id = 9;
I can not find the answer in the docs. In short:
I want to JOIN on two tables AND LATERAL VIEW EXPLODE TABLEA
Seems simple enough but throws syntax issue.
select a.col1
,b.col1
from (Select a.col1
from tableA a
lateral view explode(numcred) e as creds
where e.creds.id = 9
) a
join tableB b
on a.col1 = b.col1
Not at my computer now, so no way to test this, but my guess is you'll have to write an inner query. Something like this:
SELECT
a.col1,
b.col1
FROM (
SELECT
dummy.col1
FROM table_a dummy
LATERAL VIEW EXPLODE(numcred) tableA as creds
WHERE
creds.id = 9
) a
JOIN tableB b
ON
a.col1 = b.col1

How can I search for values in three columns, but return rows that don't match all?

I have two tables, say A and B. I wish to compare three or more columns in both tables and to return any rows in table B that don't match all of the compared columns.
I've looked at doing a left join function from recommendations, but can't quite figure it out.
Please help!
You can use left join or not exists for this. Here is one method:
select b.*
from tableb as b
where not exists (select 1
from tablea as a
where a.col1 = b.col1 and a.col2 = b.col2 and a.col3 = b.col3
);
how about something like this
Select b.col1,b.col2,b.col3 from
tableb b left outer join tablea a
on ( b.col1 != a.co11 and b.col2 != a.co12 and b.col3 != a.co13 )

using with as and nesting sql

So I have a Q that is like this:
with t1 as (
a.col1 as 'c1',
a.col2 as 'c2',
b.col1 as 'c3',
b.col2 as 'c4'
from table1 a left join table2 b
on a.col1 = b.col1
)
select
c.c1,
c.c2,
c.c3,
c.c4
from t1 c
and I want to make this whole thing a with as T2 so I can pull from what is the outer query on the above code. this is needed to perform calculations on data then renaming the column then performing calculation on the renamed columns and then one more time. I can't seem to figure out how to make the whole statement a "table" that I can then make my select statement from.
I have tried nesting another ;with as () and either it's not possible or I'm not doing it right and my guess is the latter.
Thanks in advance!
Is this what you want?
with t1 as (
select a.col1 as c1, a.col2 as c2, b.col1 as c3, b.col2 as c4
from table1 a left join
table2 b
on a.col1 = b.col1
),
t2 as (
select c.c1, c.c2, c.c3, c.c4
from t1 c
)
select *
from t2;
You can define multiple CTEs with a with statement. They are separated by commas.