Pergunta

I want the MIN(date) in the range for the each group calendarID/structureID and has a -1 value and grade 14. For instance calendarID 15555 and StructureiD 20391 would have the min(date) 5/28/2014.

DAY TABLE

date        instruction grade  calendarID  structureID
5/27/2014        0          14   15555        20391
5/28/2014       -1          14   15555        20391
5/29/2014       -1          14   15555        20391
5/30/2014        0          14   15555        20391
8/14/2013       -1          14   15516        19996
8/15/2013       -1          14   15516        19996
8/16/2013       -1          14   15516        19996
8/19/2013       -1          14   15516        19996
8/20/2013       -1          14   15516        19996
Foi útil?

Solução

SELECT MIN([date]) 
FROM [DAY] 
WHERE instruction = '-1' 
  AND grade = '14' 
GROUP BY calendarID
       , structureID

Outras dicas

Here is the result mapped for each row

select a.*,b.mindate
from DayTable a
inner join 
(select calendarID,structureID,min([date]) as mindate
from DayTable
where instruction=-1 and grade=14
group by calendarID,structureID) b
on a.calendarID=b.calendarID and a.structureID=b.structureID

or

with b as
(select calendarID,structureID,min([date]) as mindate
from DayTable
where instruction=-1 and grade=14
group by calendarID,structureID)
select a.*,b.mindate
from DayTable a
inner join b
on a.calendarID=b.calendarID and a.structureID=b.structureID
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top