Query left join (1 Viewer)

Anetal

New member
Local time
Today, 08:58
Joined
Jan 4, 2021
Messages
10
Hi,

I have a problem. I need to join two tables with a query. A need all information from table 1 and Value for every ID from table 2 - but only from max available year.
How to do it?

Table 1
idname
1​
A
2​
B
3​
C
4​
D
5​
E

Table 2
idyearvalue
1​
2020​
100​
1​
2019​
50​
1​
2018​
0​
2​
2019​
200​
2​
2018​
100​
3​
2015​
300​
 

onur_can

Active member
Local time
Yesterday, 23:58
Joined
Oct 4, 2015
Messages
180
Then your Table2 should be the one on the left. In this case, you must add Year to Table1 and create 1 to many relations in between.
 

isladogs

MVP / VIP
Local time
Today, 07:58
Joined
Jan 14, 2017
Messages
18,218
I'm obviously reading your post in a different way to @onur_can as its clear to me that Table1 (all records from...) is on the left side of the join.
I think this is what you want:

Code:
SELECT Table1.ID, Table1.name, Max(Table2.Year) AS MaxOfyear, First(Table2.Value) AS FirstOfValue
FROM Table1 LEFT JOIN Table2 ON (Table1.ID = Table2.id)
GROUP BY Table1.ID, Table1.name;

However, your query criteria aren't clear.
If that doesn't give the expected results, please can you state what results your query should have based on that data.

Also Name, Year, Value are all reserved words and should not be used as field names
 
Last edited:

Anetal

New member
Local time
Today, 08:58
Joined
Jan 4, 2021
Messages
10
I'm obviously reading your post in a different way to @onur_can as its clear to me that Table1 (all records from...) is on the left side of the join.
I think this is what you want:

Code:
SELECT Table1.ID, Table1.name, Max(Table2.Year) AS MaxOfyear, First(Table2.Value) AS FirstOfValue
FROM Table1 LEFT JOIN Table2 ON (Table1.ID = Table2.id)
GROUP BY Table1.ID, Table1.name;

However, your query criteria aren't clear.
If that doesn't give the expected results, please can you state what results your query should have based on that data.

Also Name, Year, Value are all reserved words and should not be used as field names
Thank you very much.


I tried the sample data method but got the wrong result. I am attaching a file

1610292053280.png
 

Attachments

  • First.accdb
    412 KB · Views: 184
  • 1610291968867.png
    1610291968867.png
    30.3 KB · Views: 440

isladogs

MVP / VIP
Local time
Today, 07:58
Joined
Jan 14, 2017
Messages
18,218
I can't test yours as you are using linked Excel tables which obviously won't work for me.
I've attached the test db I created before answering. The results I get, using your original data are:
1610293717581.png

You modified your original data after posting...
 

Attachments

  • Anetal-Test.zip
    18.9 KB · Views: 328

Anetal

New member
Local time
Today, 08:58
Joined
Jan 4, 2021
Messages
10
For id=2 , max year should be 2020 and value for this year 1000.
In query year is 2020 and value 100.



1610294930033.png
 

Attachments

  • First.accdb
    644 KB · Views: 436

plog

Banishment Pending
Local time
Today, 01:58
Joined
May 11, 2011
Messages
11,646
You need a subquery to determine the latest year for each id:

Code:
SELECT Table2.id, Max(Table2.Year) AS LatestYear
FROM Table2
GROUP BY Table2.id;

Paste that into a new query object and save it as 'sub1'. Then to get the results you want use the below SQL:

Code:
SELECT Table1.id, sub1.LatestYear, Table2.value
FROM (Table1 LEFT JOIN sub1 ON Table1.id = sub1.id) LEFT JOIN Table2 ON (sub1.LatestYear = Table2.year) AND (sub1.id = Table2.id);
 

Users who are viewing this thread

Top Bottom