I've two related tables:
Table1
Id
-----
1
2
3
Table2
Id Feature
--------------
1 Car
1 Moto
1 Camper
2 Moto
2 Scooter
3 Apple
I want to select Ids which have, for example, both 'Car' AND 'Moto'.
So in the example i want to get only Id = 1.
Use the INTERSECT operator:
select id from table2 where feature = 'Car'
intersect
select id from table2 where feature = 'Moto'
This:
WITH features AS
(
SELECT feature
FROM (
VALUES
('Car'),
('Moto')
) q (feature)
)
SELECT *
FROM table1 t1
WHERE NOT EXISTS
(
SELECT feature
FROM features
EXCEPT
SELECT feature
FROM table2 t2
WHERE t2.id = t1.id
)
or this:
SELECT *
FROM table t1
WHERE (
SELECT COUNT(*)
FROM table2 t2
WHERE t2.id = t1.id
AND t2.feature IN ('Car', 'Moto')
) = 2
Which query is more efficient depends on how many records you have in both tables and how many matches there are.
This select does two LEFT OUTER JOINs to table2 (one based on 'Car' and the other based on 'Moto') and makes sure that each JOIN returned a result. The DISTINCT ensures that you get each ID only once.
SELECT DISTINCT t1.id
FROM table2 t2
LEFT OUTER JOIN table2 t2_2 ON t2.id = t2_2.id AND t2_2.feature = 'Moto'
WHERE t2.feature = 'Car'
AND t2_2.id IS NOT NULL
Edit: Removed join to table1 since it really isn't needed.
Related
I'm creating 1 temp table (temp1) using table1.
and I want to check if data from temp table is present in table1 and table2.
table1 and table2 have same columns.
It's difficult to assess exactly what you need without further detail, but you could try a LEFT JOIN and a COUNT here to indicate whether there are any matching rows (whereby anything over 0 indicates matching rows)
SELECT
COUNT(*) AS matching_rows
FROM
(
SELECT
1 AS 'ColumnA'
) AS T1
LEFT OUTER JOIN
(
SELECT
2 AS 'ColumnA'
) AS T2
ON T1.ColumnA = T2.ColumnA
WHERE
T2.ColumnA IS NOT NULL
You can also use an INNER JOIN for this:
SELECT
COUNT(*) AS matching_rows
FROM
(
SELECT
1 AS 'ColumnA'
) AS T1
INNER JOIN
(
SELECT
2 AS 'ColumnA'
) AS T2
ON T1.ColumnA = T2.ColumnA
I'm new to mssql .Here am trying to get values from database by joining three tables .
Table 1:
Table 2 :
Here the image there is a possibility for a single user can have multiple image id form this I need to take any one of the image.
Table 3 :
Here am joining the Table 1 and Table 2 by using H_ID
and Table 2 and Table 3 by using IMG_ID.
What I want to do is Need to get all the colum values from Table 1 and Table 2 But the first URL from the Table 3.
In this case an employee has multiple images in the Table I need to take the 1 URL.
Result should be like this :
Query :
SELECT T1.H_ID AS 'ID',
T1.NAME,
T1.ROLE,
T2.SALARY,
T3.IMAGE
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
JOIN TABLE3 T3
ON T3.IMG_ID = T2.IMG_ID
WHERE T1.STATUS = 'ACTIVE'
Now this query returns 3 rows for the id H_ID = 1001 but It should be a single row.
Can anyone help me to fix this .
use row_number()
with cte as
(SELECT T1.H_ID AS 'ID',T1.NAME,T1.ROLE,T2.SALARY,T3.IMAGE
,row_number() over(partition by T2.img_id order by T3.id) rn
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
JOIN TABLE3 T3
ON T3.IMG_ID = T2.IMG_ID WHERE T1.STATUS = 'ACTIVE'
) select * from cte where rn=1
After you comments it seems you need subquery
select T1.*,T2.sal,a.url
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
left join ( select min(id),img_id,url from table3 group by img_id,url) a
on T2.IMG_ID= a.img_id
WHERE T1.STATUS = 'ACTIVE'
I think, You can simply use OUTER APPLY and TOP 1 for that
SELECT T1.H_ID AS 'ID',
T1.NAME,
T1.ROLE,
T2.SALARY,
T3.IMAGE
FROM TABLE1 T1
JOIN TABLE2 T2 ON T1.H_ID T2.H_ID
OUTER APPLY(SELECT TOP 1 T3.IMAGE
FROM TABLE3 T3 WHERE T3.IMG_ID = T2.IMG_ID
--ORDER BY <column_name> --to take top 1 value in specific order
) T3
WHERE T1.STATUS = 'ACTIVE'
There are two table TABLE1 and TABLE2 in which there is a common field ID. I wanted to retrieve values from TABLE2 that doesnot match in TABLE1 based on ID value.
select * from TABLE2 where subject = 1 and ID NOT IN (select ID from TABLE1 where subject = 1)
Sample:
TABLE1 ID SUBJECT 1 1
TABLE2 ID SUBJECT 1 1 2 1
The expected result is 2 and it works fine.
But when TABLE1 is empty or the inner select ID from TABLE1 where subject = 1 returns empty, the whole select statement returns empty.
But the expected result is 1, 2
Is there any way to achieve this ?
Use a left join
select t2.*
from table2 t2
left outer join table1 t1 on t1.id = t2.id and t1.subject = 1
where t2.subject = 1
and t1.id is null
See a good explanation of joins
I think you can use not exists also for this work -
select * from TABLE2 where subject = 1 and NOT exists
(select 1 from TABLE1 where subject = 1 and table1.id = table2.id)
I've got the following tables:
Table1 {ArticleNo (int), ArtDescription (string) }
Table2 { ArticleNo (int), Year (date) }
Table1.ArticleNo is a primary key.
Table2.ArticleNo is a foreign key referenced to table1.ArticleNo
It's difficult to explain what I want to query, so here a short example:
Table1
(1,Desk)
(2,Chair)
(3,Ruler)
Table2
(1,2000)
(1,2000)
(2,2001)
The query should return:
1 Desk 2001
2 Chair 2000
3 Ruler 2000
3 Ruler 2001
All articles which are not sold (or whatever) in all years (all years from table2).
I hope you understand my example - the query seems to be very complex. Here my approach to a solution:
SELECT table1.ArticleNo,table1.ArtDescription,table2.Year
FROM table1
JOIN table2
ON table1.ArticleNo=table2.ArticleNo
WHERE NOT table1.ArticleNo IN (SELECT table2.Year FROM table2);
I tried lots of different things.. I hope you can help me!
SELECT t1.*, t2.year
FROM t1
CROSS JOIN
(
SELECT DISTINCT year
FROM t2
) t2
WHERE (t1.id, t2.year) NOT IN
(
SELECT t2.id, t2.year
FROM t2
)
Create an index on t2 (year, id) (in this order) for the query to work fast.
You could use a cross join to create a list of all item+year combinations. Then you could filter the rows without sales with a not exists condition:
select *
from t1 items1
cross join
(
select distinct year
from t2 sales1
) sales2
where not exists
(
select *
from t2 sales3
where sales3.ItemId = items1.ItemId
and sales3.Year = sales2.Year
)
There are a bunch of ways of doing this. Two examples:
select
t1.ArtDescription,
y.Year
from
Table1 t1
join (
select distinct
t2.Year
from
Table2 t2
) y on 1=1
where
not exists (
select
1
from
Table2 tx2
where
tx2.ArticleNo = t1.ArticleNo and tx2.Year = y.Year)
Oracle (SQL Server can do the same thing, use EXCEPT instead of MINUS):
select
t1.ArtDescription,
y.Year
from
Table1 t1
join (
select distinct
t2.Year
from
Table2 t2
) y on 1=1
MINUS
select
t12.ArtDescription
t22.Year
from
Table1 t12
join Table2 t22 on t12.ArticleNo = t22.ArticleNo
SELECT DISTINCT table1.ArticleNo,table1.ArtDescription,table2.Year
FROM table1 CROSS JOIN table2
WHERE table1.ArticleNo != table2.ArticleNo order by table1.ArticleNo;
I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.