2014-09-01 79 views
0

如何按星期對Impala查詢結果進行分組?數據是這樣的:如何按周分組Cloudera impala

userguid     eventtime 
0 66AB1405446C74F2992016E5 2014-08-01T16:43:05Z 
1 66AB1405446C74F2992016E5 2014-08-02T20:12:12Z 
2 4097483F53AB3C170A490D44 2014-08-03T18:08:50Z 
3 4097483F53AB3C170A490D44 2014-08-04T18:10:08Z 
4 4097483F53AB3C170A490D44 2014-08-05T18:14:51Z 
5 4097483F53AB3C170A490D44 2014-08-06T18:15:29Z 
6 4097483F53AB3C170A490D44 2014-08-07T18:17:15Z 
7 4097483F53AB3C170A490D44 2014-08-08T18:18:09Z 
8 4097483F53AB3C170A490D44 2014-08-09T18:18:18Z 
9 4097483F53AB3C170A490D44 2014-08-10T18:23:30Z 

預期的結果是:

date     count of different userguid 
2014-08-01~2014-08-07 40 
2014-08-08~2014-08-15 20 
2014-08-16~2014-08-23 10 

謝謝。

回答

3

如果eventtime存儲爲timestamp

SELECT TRUNC(eventtime, "D"), COUNT(DISTINCT userguid) 
FROM your_table 
GROUP BY TRUNC(eventtime, "D") 
ORDER BY TRUNC(eventtime, "D"); 

不然,如果eventtime存儲爲string

SELECT TRUNC(CAST(eventtime AS TIMESTAMP), "D"), COUNT(DISTINCT userguid) 
FROM your_table 
GROUP BY TRUNC(CAST(eventtime AS TIMESTAMP), "D") 
ORDER BY TRUNC(CAST(eventtime AS TIMESTAMP), "D"); 

有關TRUNC功能的更多信息,請參閱Cloudera Impala documentation on Date and Time Functions

+0

你能解釋一下答案嗎?如何從下週的週日到週六進行分組? – MANU 2016-08-11 14:22:24

0

在Impala中,TRUNC(時間戳,「D」)表示找到一週的開始日期。您可以查看Impala日期和時間功能here

例如:

select trunc(cast('2016-11-10' as timestamp), "D") 
+---------------------------------------------+ 
| trunc(cast('2016-11-10' as timestamp), 'd') | 
+---------------------------------------------+ 
| 2016-11-07 00:00:00       | 
+---------------------------------------------+ 

+---------------------------------------------+ 
| trunc(cast('2016-11-09' as timestamp), 'd') | 
+---------------------------------------------+ 
| 2016-11-07 00:00:00       | 
+---------------------------------------------+ 

+---------------------------------------------+ 
| trunc(cast('2016-11-11' as timestamp), 'd') | 
+---------------------------------------------+ 
| 2016-11-07 00:00:00       | 
+---------------------------------------------+