Assuming StartDate and FinishDate are datetime columns with dates only (time=00:00:00.000) and StartPeriod and FinishPeriod can have values of 1 to 48, where 1 means first half hour of day, this should do it:select Valuefrom MyTablewhere SearchDate >= -- Datetime for start of StartDate/Period dateadd(minute,(StartPeriod-1)*30,StartDate) and SearchDate < -- Datetime of first point in time after FinishDate/Period dateadd(minute,FinishPeriod*30,FinishDate)
CODO ERGO SUM