Query to get oldest date into a column? - sql

I have some tables that look like this:
Table1
Column1
A
B
C
Table2
column1 Id1 Id2
A 2 100
A 3 101
B 2 100
B 3 101
C 2 100
Table3
Id2 Date Item Status
100 10/20/17 A1 A
101 10/21/17 A1 A
100 11/22/17 A2 I
101 11/23/17 A2 A
My query looks like this:
Select
Date, *
FROM TABLE1 T1
LEFT JOIN Table2 T2 ON
T1.Column1 = T2.Column2
LEFT JOIN Table3 T3 ON
T2.Id2 = T3.Id2
I would like to return the oldest date on table 3 where the status is A into the date column in my query.
I was able to return the oldest date with this query but can't integrate this into my query with joins.
SELECT
MIN(DATE)
FROM Table3
WHERE Id2 IN (100,101)
AND STATUS = 'A'
group by Id2, ITEM
How can I get the oldest date into the query that uses the join clauses?

Couple of ways to approach what I believe that you're trying to do.
I'd say the easiest would be to simplify your search parameters with a subquery of Table 3.
select * from table2 t2
LEFT JOIN
(Select Id2, Item, max(Date)from Table 3
WHERE Status = 'A'
GROUP BY Id2, Item)a ON t2.Id2 = a.Id2;
The subquery does the filtering and then a quick join to table 2 would get all your data.

Related

select data where two column have the same data in two different tables

I have two tables, and they have three column in common as shown below:
table 1
x
id2
id3
1
0
1
2
0
1
3
1
0
table 2
y
id2
id3
a
0
1
for example I want to get data from table one where the two columns id2 and id3 have the same value as table 2,
so the result would be like this:
x
id2
id3
1
0
1
2
0
1
It is simple join
select t1.*
from t1 join t2 on t1.id2 = t2.id2 and t1.id3 = t2.id3
It is another view you are looking for a filter table. You want to select some data based on defined in table 2.
Let's assume table 2 is your filter, and you can find data basically join or just select
select * from table1 t1.*
left join table2 t2 on t2.id2 = t1.id2 and t2.id3 = t1.id3

SQL Query -Without using nested subqueries

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
*/

Aggregate functions as column results from multiple tables

I have the following table structures:
Table1
--------------
Table1Id
Field1
Field2
Table2
------------
Table2Id
Table1Id
Field1
Field2
Table3
-----------
Table3Id
Table1Id
Field1
Field2
I need to be able to select all fields in Table1, count of records in Table2, and count of records in Table3 Where count of records in Table2 > count of records in Table3
Here is an example of expected output with the given data:
Table1 Data
-------------
1 Record1Field1 Record1Feild2
2 Record2Field1 Record2Feild2
3 Record3Field1 Record3Feild2
4 Record4Field1 Record4Feild2
Table2 Data
------------
1 1 Record1Field1 Record1Feild2
2 1 Record2Field1 Record2Feild2
3 2 Record3Field1 Record3Feild2
4 2 Record4Field1 Record4Feild2
5 2 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
7 4 Record6Field1 Record6Feild2
8 4 Record6Field1 Record6Feild2
Table3 Data
------------
1 2 Record1Field1 Record1Feild2
2 2 Record2Field1 Record2Feild2
3 3 Record3Field1 Record3Feild2
4 3 Record4Field1 Record4Feild2
5 3 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
Desired Results
Table1Id Field1 Field2 Table2Count Table3Count
1 Record1Field1 Record1Field2 2 0
2 Record2Field1 Recird2Field2 3 2
4 Record4Field1 Recird4Field2 3 1
Notice record 3 in Table 1 is not shown because the record count in Table2 is less than the record count in Table3. I was able to make this work using a very ugly query similar to the one below but feel there is a much better way to do this using joins.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) as Table2Count,
(Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id) as Table3Count,
From
Table1 t1
Where
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) > (Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id)
Hard to test it without working examples but something along these lines should be a good starting point.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2,
COUNT(DISTINCT t2.Table2Id),
COUNT(DISTINCT t3.Table3Id)
From Table1 t1
LEFT OUTER JOIN Table2 t2 ON t1.Table1Id = t2.Table1Id
LEFT OUTER JOIN Table3 t3 ON t1.Table1Id = t3.Table1Id
GROUP BY t1.Table1Id
HAVING COUNT(DISTINCT t2.Table2Id) > COUNT(DISTINCT t3.Table3Id)
You could get all the value in t1 and the data form t2 e t3 for your comparision using a couple of join on grouped values
SELECT
t1.Table1Id
,t1.Field1
,t1.Field2
, tt2.count_t2
, tt3.count_t3
from table1 t1
join (
select Table1Id, count(*) count_t2
From Table2
group by Table1Id
) tt2 on tt2.Table1Id = t1.Table1Id
join (
select Table1Id, count(*) count_t3
From Table3
group by Table1Id
) tt3 on tt3.Table1Id = t1.Table1Id
where tt2.count_t2 < tt3.count_t3 <

SQL Complex join not giving distinct result

I have two tables :-
Table1:-
ID1
1
1
1
1
4
5
Table2:-
Id2
2
2
1
1
1
8
I want to show all the ID2 from table2 which are present in ID1 of table1 by using joins
I used :-
select ID2 from Table2 t2 left join Table1 t1
on t2.Id2=t1.Id1
But this was giving repeated result as :-
Id2
1
1
1
1
1
1
1
It should show me 1 as 3 times only as it is present in Table2 3 times.
Please help.
You're matching the value 1 with 4 rows on Table1 and 3 rows on Table2 that's why you're seeing 12 rows. You need an additional JOIN condition. You can add a ROW_NUMBER and do an INNER JOIN to achieve your desired result.
WITH Cte1 AS(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY Id1 ORDER BY (SELECT NULL))
FROM Table1
),
Cte2 AS(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY Id2 ORDER BY (SELECT NULL))
FROM Table2
)
SELECT c2.Id2
FROM Cte2 c2
INNER JOIN Cte1 c1
ON c1.Id1 = c2.Id2
AND c1.rn = c2.rn
However, you can achieve the desired result without using a JOIN.
SELECT *
FROM Table2 t2
WHERE EXISTS(
SELECT 1 FROM Table1 t1 WHERE t1.Id1 = t2.Id2
)
It's the expected behavior of Join Operation. It will match every row from the two tables, so you will get 12 rows containing value 1 in result of join query.
You can use below query to get desired result.
select ID2 from Table2 t2 WHERE ID2 IN (SELECT ID1 FROM Table1 t1)
select id2 from table2 t2 where exists ( select 1 from table1 t1 where t1.id1 = t2.id2)
Your join logic works fine, the problem is each of your ID2 is matching against all ID1s. A simple solution would be to join with a table of distinct ID1s to avoid this duplication.
select
t2.ID2
from Table2 t2
left join (select distinct * from Table1) t1
on t1.Id1=t2.Id2
where t1.ID1 is not null
;
Here is a functional example
This will select your entire ID2 list with ID1 populated in a column. ID1 is null where there was no match. Select your ID2 column from this table but just don't pull null values (with where clause):

Return null for second duplicate row in JOIN in Access

I saw few posts for avoiding duplicates. But this is not about removing duplicates from the output. but keeping duplicates from one table but avoiding rows repeating from the joining table.
So I have 2 table Table1 and Table2 . Table1 has duplicate values for ID column. Table2 does not have duplicate values for ID column. I wish to join both tables. In the result I want all rows from Table1 including duplicates but i do not want Table2 to repeat rows for each duplicate row in Table1.
Table1
ID Column2
1 A
1 B
2 C
3 D
Table2
ID Column3
1 X
2 Y
My Query is
Select A.ID,A.Column2,B.ID,B.Column3 from Table1 A LEFT JOIN Table2 B on A.ID=B.ID
The result i get is
ID Column2 ID Column3
1 A 1 X
1 B 1 X
2 C 2 Y
3 D null null
The expected result is
ID Column2 ID Column3
1 A 1 X
1 B null null
2 C 2 Y
3 D null null
i could not see any possibilities in access database .
Please let me know if there is any possibility of getting this result
I can not check right now, but you will get the idea:
select t11.id, t11.col2, t2.id, t2.col3
from (table1 t11
left join (select id, min(col2) as col2 from table1 group by id) t12
on t11.id = t12.id and t11.col2 = t12.col2)
left join table2 t2 on t12.id = t2.id