我想建立一個查詢,將拉動在給定的日期範圍和時間範圍內創建的行數。換一種說法。日期範圍將爲給定的月份,但我只想要在給定時間內(例如從08:00 - 17:00)在整個月內每天創建的行。如何查詢MS SQL中給定日期範圍的「每日」結果?
這很容易嗎?
我在想每一天的子查詢,但不知道是否有更簡單的方法。
我想建立一個查詢,將拉動在給定的日期範圍和時間範圍內創建的行數。換一種說法。日期範圍將爲給定的月份,但我只想要在給定時間內(例如從08:00 - 17:00)在整個月內每天創建的行。如何查詢MS SQL中給定日期範圍的「每日」結果?
這很容易嗎?
我在想每一天的子查詢,但不知道是否有更簡單的方法。
這會給你你想要的,我想。您可以創建一個存儲過程或函數並傳入開始/結束日期和開始/結束小時(現在已修復)。注意在結束日期嚴格少於。您也可以使用小於或等於'8/31/2009 11:59:59'。
select count(*),
datepart(year,created) as [year],
datepart(month,created) as [month],
datepart(day,created) as [day]
from table
where created >= '8/1/2009'
and created < '9/1/2009'
and datepart(hour,created) >= 8
and datepart(hour,created) <= 17
group by datepart(year,created), datepart(month,created), datepart(day,created)
使用DATEPART()函數來測試你有興趣小時
例如:
select 1,
datepart(hour,getDate())
where datepart(hour,getDate()) >= 8 and datepart(hour,getDate()) < 17
可以做這樣的事情這樣做它的時間範圍( 12PM - 1PM)and days(today to 10 days ago):
select * from table where DateColumn < GetDate() and DateColumn > (GetDate() - 11) and DatePart(hh, DateColumn) >= 12 and DatePart(hh, DateColumn) <= 13
爲什麼不只是這樣做呢?
... WHERE MONTH(TheDateColum) = @Month AND HOUR(TheDateColum) >= 8 AND HOUR(TheDateColumn) <= 17
嗯...他們打敗了我。
謝謝大家的回覆! – jktravis 2009-09-03 17:10:10