문제

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
도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top