簡體   English   中英

Oracle 日期/時間操作查詢

[英]Oracle Date/Time manipulation query

我在 oracle SQL 中為我的查詢找到解決方案時遇到問題:

這是我需要的查詢示例:

Select Result_Date as hour , Type , Count(Id) as Number From Results 
Where Result_Date Between Today'06:00' And today'14:00'
group by result_date , type 

結果日期包含小時,結果日期的格式為timestamp。 我如何在不輸入 between 語句中的日期的情況下完成此查詢,只輸入我想搜索今天之間的小時數。

如果您有任何想法,請告訴我,歡迎提供任何想法。

您可以在時間戳上使用trunc()函數,因此這可能會執行您想要的操作:

Select trunc(Result_Date, 'HH24') as hour , Type , Count(Id) as Number From Results 
Where trunc(Result_Date, 'HH24') Between '06:00' And '14:00' and
      trunc(Result_Date) = trunc(sysdate)
group by trunc(Result_Date, 'HH24'), type ;

筆記:

trunc()日期默認為當天。 如果您願意,可以為此添加特定格式。

編輯,這對我有用

   Select to_number(extract(hour from Result_Date)) as hour , Type , Count(Id) as Number From Results Where To_Date(Trunc(Result_Date)) = To_Date(Trunc(Current_Timestamp))
        and to_number(extract(hour from Result_Date)) Between 6 and 14     
            group by  to_number(extract(hour from )Result_Date) as hour  , type 

@Gordon Linoff ,謝謝你對主干的建議,它幫助我分配

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM