I edited my answer so the solution for my question is:
select DISTINCT e.Cod_proiect
, p.Descriere
, p.Stare
, p.ClientService
, e.Data
from ExpPoz as e
join Proiecte as p
on e.Cod_proiect=p.Cod_proiect
join (
select cod_proiect, max(data) maxDt
from ExpPoz
group by cod_proiect
) latest on latest.cod_proiect = e.cod_proiect and latest.maxDt = e.Data and p.Stare='I'
Which gives the following error: Subquery returned more than 1 value I am trying to get every line with the maximum date.
I have the following table structure:
a date1
a date2
a date3
b date4
b date5
The output should be, supposing that date3 and date5 are the oldest/biggest:
a date3
b date5
Thanks in advance.
This should do the trick:
select e.Cod_proiect
, p.Descriere
, p.Stare
, p.ClientService
from ExpPoz as e join Proiecte as p on e.Cod_proiect=p.Cod_proiect
join (
select cod_proiect, max(data) maxDt
from ExpPoz
group by cod_proiect
) latest on latest.cod_proiect = e.cod_proiect and latest.maxDt = e.Data
where p.Stare='I'
Please note that the way you have written the joins
is very old and it is better and more clear if you use the modern join
style. In this query I selected everything you need and joined once with the latest record per cod_proiect
.
You might try this:
SELECT cod_proiect,
MAX(data)
FROM
(
SELECT DISTINCT
ep.cod_proiect,
ep.data
FROM ExpPoz ep
JOIN Proiecte pr
ON ep.cod_proiect = pr.cod_proiect
AND pr.stare = 'I'
)
GROUP BY cod_proiect;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.