0

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.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
user1147188
  • 135
  • 2
  • 3
  • 12

2 Answers2

1

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.

Rigerta
  • 3,959
  • 15
  • 26
0

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;
Robert Kock
  • 5,795
  • 1
  • 12
  • 20