從2015-01-01開始到2015-01-31,提取每天22點到第二天9點的數據,注意有開始時間(START_TIME)和結束時間(END_TIME)
我的sql:
select A.BILLING,sum(out + in) from inf.EVEN_TGAS A
where A.START_TIME between '2015-01-01 22:00:00' and '2015-01-31 09:00:00'
and A.end_TIME between '2015-01-01 22:00:00' and '2015-01-31 09:00:00'
GROUP BY A.BILLING
limit 10;
怎麼才能不提取比如像2015-01-02 14:23:50那個時間點的數據呢???
麻煩各位大神了
你試下這樣是否滿足你的要求,按照你所寫的內容,因為有開始時間和結束時間,所以會有很多數據時間上是處於交叉的,
如開始時間滿足時間要求,但是結束時間不在咱們的時間段內,這些我都給算成是不統計的數據了。
select A.BILLING,sum(out + in) from inf.EVEN_TGAS A
where (SUBSTRING(A.START_TIME,1,11) >= '2015-01-01' and SUBSTRING(A.START_TIME,1,11)<= '2015-01-31')--判斷開始日期是否符合時間段
and (SUBSTRING(A.end_TIME,1,11) >= '2015-01-01' and SUBSTRING(A.end_TIME,1,11)<= '2015-01-31')--判斷結束日期是否符合時間段
and (
(SUBSTRING(A.START_TIME,12,8) >= '22:00:00' and SUBSTRING(A.START_TIME,12,8)<= '23:59:59')
or
(SUBSTRING(A.START_TIME,12,8) >= '00:00:00' and SUBSTRING(A.START_TIME,12,8)<= '09:00:00')
)and (
(SUBSTRING(A.end_TIME,12,8) >= '22:00:00' and SUBSTRING(A.end_TIME,12,8)<= '23:59:59')
or
(SUBSTRING(A.end_TIME,12,8) >= '00:00:00' and SUBSTRING(A.end_TIME,12,8)<= '09:00:00')
)and (
datediff(second,A.START_TIME,A.end_TIME) <= 39600 -- 保證時間不是跨度好多天的數據
)
GROUP BY A.BILLING
limit 10;
語句比較繁雜,你可以試著優化優化