2017-03-23 94 views
0

數據以UTC格式存儲在數據庫中,我必須將本地轉換爲UTC獲取,然後在當地時間顯示結果。 現在我來自印度,所以如果我想今天搜索數據,我必須從3月22日下午6點30分UTC到3月23日6:30 UTC查詢。現在說我想檢查一週中的每一天的數據,直到現在是當地時間下午4:30。現在我寫此查詢[簡單地說我的目的是從上午12點得到每天的參觀人數到下午4:30]從2日期獲取具體數據的時間範圍

SELECT 
      FLOOR(TIMESTAMPDIFF(HOUR, "2017-03-16 18:29:59", 
      visit.date_created)/24) as dayofweek, 
      DAYOFWEEK(visit.date_created) day_num, 
    @rownum := @rownum + 1 as date_created_set_av, 
      count(distinct(visit.pkey)) AS Visits, 
      sum(revenue) AS Revenue, 
      sum(revenue)/count(distinct(visit.pkey)) as EPC 
      FROM la_20.visit, la_20.action 
      cross join (select @rownum := 0) r 
      WHERE visit.pkey=action.pkey and (visit.is_bot = 0) 
      AND visit.date_created >="2017-03-16 18:29:59" 
      AND visit.date_created <="2017-03-23 18:29:59" 
      AND TIME(visit.date_created)<="16:30:00" 
    GROUP BY dayofweek order by day_num 

,但它的作用是從2017年3月23日00取值:00:00至2017-03-23 16:30:00。我需要的是從18點29分59秒到16點30分顯示第16日至第23日日常數據的結果。記住我需要每天的結果,而不是一天的結果。任何人都可以幫助

回答

0

您是否嘗試過以下where子句?

WHERE visit.pkey=action.pkey and (visit.is_bot = 0) 
     AND visit.date_created >="2017-03-22 18:29:59" 
     AND visit.date_created <="2017-03-23 14:29:59" 

3.編輯
OK,因爲OP又想不同的東西 - 爲什麼不;-) - 這裏是另一個解決方案,可能(也可能不會)解決他的問題:

SELECT 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -7 day) AND addtime(adddate(curdate(), interval -7 day),'16:00:00') 
      THEN 1 END) dm7, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -6 day) AND addtime(adddate(curdate(), interval -6 day),'16:00:00') 
      THEN 1 END) dm6, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -5 day) AND addtime(adddate(curdate(), interval -5 day),'16:00:00') 
      THEN 1 END) dm5, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -4 day) AND addtime(adddate(curdate(), interval -4 day),'16:00:00') 
      THEN 1 END) dm4, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -3 day) AND addtime(adddate(curdate(), interval -3 day),'16:00:00') 
      THEN 1 END) dm3, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -2 day) AND addtime(adddate(curdate(), interval -2 day),'16:00:00') 
      THEN 1 END) dm2, 
COUNT(CASE WHEN d between 
     adddate(curdate(), interval -1 day) AND addtime(adddate(curdate(), interval -1 day),'16:00:00') 
      THEN 1 END) dm1, 
COUNT(CASE WHEN d between 
       curdate()     AND addtime(  curdate()     ,'16:00:00') 
      THEN 1 END) today 
FROM (select  '2017-03-17 15:00:01' d 
union all select '2017-03-19 14:00:01' 
union all select '2017-03-19 12:00:01' 
union all select '2017-03-19 13:00:01' 
union all select '2017-03-19 11:00:01' 
union all select '2017-03-20 11:20:01' 
union all select '2017-03-20 11:30:01' 
union all select '2017-03-20 10:40:01' 
union all select '2017-03-20 10:23:01') dates 

這是一個獨立的查詢,它直接處理子查詢中給出的示例數據。這導致下面的列表( 「DM1」=天減1):

dm7 | dm6 | dm5 | dm4 | dm3 | dm2 | dm1 | today 
    1 | 0 | 4 | 4 | 0 | 0 | 0 |  0 

你可以在這裏進行測試:http://rextester.com/ITLBL24540

+0

我編輯了我的問題一點,我想要的是在特定的實例上,比如下午4:00我的本地時間我想檢查每天的數據,所以中午12:00午夜到下午4:00中午我想查看日常數據。現在它的功能是顯示當天的數據直到4點,但對於其他數據則顯示全天的數據。 – user2972115

+0

我的問題可能並不清楚1)我正在嘗試檢索7天的結果2)我想顯示從午夜12點到下午4點的每日數據的數據所以例如在下午4點,什麼是訪問者#在每週的每一天,昨天,前天等等。感謝您的幫助,花時間回答我的問題。 – user2972115

+0

謝謝。這工作! – user2972115

0

你應該改變你的WHERE子句到

WHERE visit.pkey=action.pkey and (visit.is_bot = 0) 
AND visit.date_created >="2017-03-22 18:29:59" and visit.date_created <="2017-03-23 14:29:59" 

已更新

  • 今天午夜:DATEADD(d,0,DATEDIFF(d,0,GETDATE()) )
  • 今天下午4點:DATEADD(HOUR,16,DATEADD(d,0,DATEDIFF(d,0,GETDATE())))

    WHERE visit.pkey=action.pkey and (visit.is_bot = 0) 
    AND visit.date_created >= DATEADD(d,0,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,0,DATEDIFF(d,0,GETDATE()))) 
    

更新2: 然後你我將不得不像這樣擴展你的where子句。

WHERE visit.pkey=action.pkey and (visit.is_bot = 0) 
AND (
    (visit.date_created >= DATEADD(d, 0,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d, 0,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-1,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-1,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-2,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-2,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-3,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-3,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-4,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-4,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-5,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-5,DATEDIFF(d,0,GETDATE())))) 
OR (visit.date_created >= DATEADD(d,-6,DATEDIFF(d,0,GETDATE())) and visit.date_created <= DATEADD(HOUR, 16, DATEADD(d,-6,DATEDIFF(d,0,GETDATE())))) 
) 
+0

我編輯了我的問題一點,我想要的是一個特定的實例,比如下午4點我的本地時間我想檢查每天的數據,所以午夜12:00到中午4:00我想查看日常數據。現在它的功能是顯示今天的數據直到4點,但是對於其他人顯示的是全天的數據 – user2972115

+0

我的問題可能不明確1)我試圖檢索7天的結果2)我想顯示從午夜12點到下午4點每天的數據所以例如下午4點,訪客#在一週中的每一天,今天,昨天,前天等等是什麼。感謝您的幫助花時間回答我的問題 – user2972115

+0

我更新了我的答案,請參閱更新2. – Kevin

相關問題