I have two tables that look like the following:
Table 1 (IDs):
ID
1
2
3
4
5
Table 2 (Categories):
ID CAT
1 A
1 B
2 A
3 A
4 B
5 A
5 B
5 C
What I would like to do is join table 2 to table 1 and only keep instances where there is 1) Only one match 2) That match is CAT = A
So my final table would look like:
ID CAT
2 A
3 A
One method is aggregation and having:
select id, max(cat)
from t
group by id
having count(*) = 1 and
max(cat) = 'A';
You can use a subquery with not exists:
select t1.id, t2.cat
from table1 t1
join table2 t2 on t1.id = t2.id
where t2.cat = 'A' and not exists (select 1 from table2 t3 where t3.id = t1.id and t3.cat != 'A');
Output:
id
cat
2
A
3
A
Related
I have such tables:
Table1
ID | Name
---------
1 Name1
2 Name2
3 Name3
Table2
ID | FindID Table1ID
--------------------
1 1 1
2 2 1
3 1 2
4 2 2
5 1 3
6 2 3
7 3 3
8 1 4
Table3
ID
--
1
2
What I need to do - I need to find all Table 1 Ids which have in Table 2 all Table 3 values as FindId column's values .
For example, if in Table 3 we have 1 and 2, in result it should return
Result
ID
-----
1
2
3
Because Table1.Id = 1 have 1 and 2 in Table 2, same for Table1.Id = 2, and Table1.Id = 3 have 1,2,3.
Table1.Id = 4 has only 1, so it isn't contained in results.
If in Table 3 we have 1,2,3 - then only Table1.Id = 3 will be returned.
I've tried something like
SELECT distinct t1.id FROM Table1 t1
join Table2 t2 on t2.Table1Id = t1.ID
join Table3 t3 on t3.Id = t2.FindId
But it doesn't work.
If I understand correctly, you want all table2.table1id that have all the findIds in table3. If so, aggregation should do what you want:
select t2.table1id
from table2 t2 join
table1 t3
on t2.findid = t3.id
group by t2.table1id
having count(*) = (select count(*) from table3);
I need to select registers from table that not exist in the same table. I mean if i have this table:
ID VALUE1 VALUE2 VALUE3
1 1 1 1
2 2 2 1
3 3 4 1
4 1 5 1
5 2 2 2
6 3 4 2
7 1 8 2
8 2 2 2
The result of the query should be
ID VALUE1 VALUE2 VALUE3
1 1 1 1
4 1 5 1
Because the rest of the values are the same for the value1 and value2 but diferent value3. I mean the row 2 of the table is the same that the row 5.
I try to do something like but not works:
select t1.value1, t1.value2 from table1 t1 where value3=1
and not exist
(select t2.value1, t2.value2 from table2 t2
where t1.value1=t2.value1 and t1.value2=t2.value2 and value3=2)
Thank you in advise and sorry for my english
You can use the NOT EXISTS as follows:
SELECT *
FROM YOUR_TABLE T1
WHERE T1.VALUE3 = 1
AND NOT EXISTS
(SELECT 1
FROM YOUR_TABLE T2
WHERE T1.VALUE1 = T2.VALUE1
AND T1.VALUE2 = T2.VALUE2)
I think not exists does what you want:
select t1.*
from table1 t1
where t1.value3 = 1 and
not exist (select 1
from table2 t2
where t2.value1 = t1.value1 and
t2.value2 = t1.value2 and
t2.value3 = 2
);
That said, you can also use window functions:
select t1.*
from (select t1.*,
max(value3) over (partition by value1, value2) as max_value3
from table1 t1
where value3 in (1, 2)
) t1
where max_value3 = 1;
I think that you are on the right track with exists.
I would phrase your query like:
select t1.*
from table1 t1
where
t1.value3 = 1
and not exist (
select 1 from table1 t2
where t1.value1 = t2.value1 and t1.value2 = t2.value2 and t2.value3 = 2
)
Key points:
the exists subquery should be from table1 (your query uses table2 but it seems like this table does not actually exists)
you don't actually need to return columns from the exists subquery, since all it does is check if some row is produced - hence the select 1
Table1
ID SystemID Description
---------------------------
1 25 Test1
1 25 Test2
2 40 Test1
2 40 Test3
3 26 Test9
3 36 Test5
4 70 Test2
4 70 Test9
Table2
ID Department
------------------
1 Sales
2 Marketing
3 Accounting
4 Purchasing
I have these 2 tables, Table1 and Table2.
I need to select all the distinct ids from Table1 that have the same description as ID = 1 and SystemID = 25, and then select all the rows from Table2 from the query result.
Is there a better way to query for this, without using nested subqueries?
select *
from Table2
where ID in (select distinct(ID)
from Table1
where SystemID = 25
and Description in (select Description
from Table1
where ID = 1 and SystemID = 25))
Final output is
1 Sales
2 Marketing
4 Purchasing
Any help is appreciated. Thank you.
I think you want:
select t1.id, t2.department
from table1 t1 join
table2 t2
on t1.id = t2.id
where t1.description in (select tt1.description from table1 tt1 where tt1.id = 1 and tt1.systemid = 25);
This is standard SQL and should work in both SQL Server and Oracle.
You can also use a modification of an outer join to detect presence of a value.
SELECT DISTINCT t2.ID, t2.DEPARTMENT
FROM
table2 AS t2
INNER JOIN table1 AS t1a ON table2.ID = table1.ID
LEFT OUTER JOIN table1 AS t1b ON t1b.id = 1 AND t1b.systemID = 25 AND t1b.description = t1a.description
WHERE t1b.ID IS NOT NULL
AND t1a.systemID = 25
This will filter out all entries who don't have a description matching an entry with id 1 and systemID 25
I believe this should give you the same result. Instead of using an IN I used an EXISTS and then instead of a futher subquery you can then use a JOIN:
SELECT *
FROM Table2 T2
WHERE EXISTS (SELECT 1
FROM Table1 T1
JOIN Table1 T1t ON T1.[Description] = T1t.[Description]
WHERE T1.ID = T2.ID
AND T1t.ID = 1 AND T1t.SystemID = 25);
SELECT DISTINCT T2.* --Use a distinct for simplicity but a group by is better
FROM Table2 AS T2
INNER JOIN Table1 AS T1_Source ON T1_Source.SystemID = 25 AND T1_Source.ID = 1
/*^ Find table1 with System and ID
Expected Result
ID SystemID Description
1 25 Test1
1 25 Test2
Note Rows are duplicated use distinct or group by
*/
INNER JOIN Table1 AS T1_Target ON T1_Target.Description = T1_Source.Description
/*^ Find table1 with all the Description matching the result we found
Expected Result
ID SystemID Description
1 25 Test1
1 25 Test2
2 40 Test1
4 70 Test2
Note Rows are duplicated use distinct or group by
*/
I have 3 tables containing data as below and I want to group data into single output.
table1
pid val
1 1
2 2
table2
id pid val
1 1 1
2 1 2
3 2 1
table3
id pid val
1 1 1
2 1 2
3 2 1
4 2 1
Required Output:
pid output
1 1 (table1 val + table2 Sum(val) - table3 Sum(val))
2 1 (table1 val + table2 Sum(val) - table3 Sum(val))
Please help with optimal query which can achieve this without any temp table.
Assume that pid is primary key of table1, you could use this
SELECT
t1.pid,
t1.val + t2.sumval - t3.sumval
FROM
table1 t1
INNER JOIN
(SELECT
pid, SUM(val) as sumval
FROM
table2
GROUP BY
pid) t2
ON
t2.pid = t1.pid
INNER JOIN
(SELECT
pid, SUM(val) as sumval
FROM
table3
GROUP BY
pid) t3
ON
t3.pid = t1.pid;
I have two tables:
TABLE 1
ID VALUE
1 ABC
2 DEF
3 GHI
4 JKL
5 XYZ
TABLE 2
ID T1_ID VALUE
1 1 A
2 1 B
3 2 A
4 3 A
5 3 B
6 4 B
I want to select all rows from TABLE 1 which have a TABLE 2 row for Values A AND B.
This would be rows 1 and 3 (not 2 because it has only A, not 4 because it has only B).
Can I do this without a subquery?
(Note: I also need to query against values in table 1 so I can't just query table 2.)
Tadaaah! Without a subquery.
select distinct
t1.*
from
Table1 t1
inner join Table2 t2a on t2a.t1_ID = t1.ID and t2a.VALUE = 'A'
inner join Table2 t2b on t2b.t1_ID = t1.ID and t2b.VALUE = 'B'
SELECT t1.ID,
t1.VALUE
FROM Table1 t1
JOIN Table2 t2
ON t1.ID = t2.T1_ID
WHERE t2.VALUE IN ( 'A', 'B' )
GROUP BY t1.ID,
t1.VALUE
HAVING COUNT(DISTINCT t2.VALUE) = 2