Query left join

Anetal

New member
Local time
Tomorrow, 00:04
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​
 
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.
 
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:
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
    First.accdb
    412 KB · Views: 231
  • 1610291968867.png
    1610291968867.png
    30.3 KB · Views: 482
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

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

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

Back
Top Bottom