我有一个表按 UNIX 时间记录具有时间字段的事务,因此每一行都是一个事务。现在我想知道一天中哪一秒的交易量最大?
结果应该是:
2013 年 10 月 1 日 20:02:34 45 ->(在第 1 天,20:02:34,我们最多有 45 笔交易,依此类推) 2013 年 10 月 2 日 12:34:21 99 2013 年 10 月 3 日 15:02:33 70
请帮助,提前谢谢。
一种方法
SELECT FROM_UNIXTIME(time) time, MAX(tcount) tcount
FROM
(
SELECT time, COUNT(*) tcount
FROM transactions
GROUP BY time
) q
GROUP BY DATE(FROM_UNIXTIME(time))
这是SQLFiddle演示
现在,如果您希望能够查看每日热门交易的时间戳,那么您需要模拟DENSE_RANK()
分析功能。一种方法
SELECT FROM_UNIXTIME(a.time) time, a.tcount
FROM
(
SELECT time, COUNT(*) tcount
FROM transactions
GROUP BY time
) a JOIN
(
SELECT DATE(FROM_UNIXTIME(time)) date, MAX(tcount) tcount
FROM
(
SELECT time, COUNT(*) tcount
FROM transactions
GROUP BY time
) q
GROUP BY DATE(FROM_UNIXTIME(time))
) b
ON DATE(FROM_UNIXTIME(a.time)) = b.date
AND a.tcount = b.tcount;
这是SQLFiddle演示
注意:在此示例中,您有两个时间戳,它们在第一天的事务数最大值相同
select yourtimecolumn,max(totalTrans) from (
select yourtimecolumn, count(id) as totalTrans
from yourtable
group by yourtimecolumn)a
group by day(yourtimecolumn)
SELECT DISTINCT your_time_column_name FROM table_name;
SELECT * FROM table_name WHERE your_time_column_name="time"
SELECT MAX(COUNT_NO), TIME_COLUMN FROM (SELECT COUNT(*) COUNT_NO ,TIME_COLUMN FROM
TABLE_NAME
GROUP BY TIME_COLUMN)
试试这个。
试试下面的查询
Select time, Max(count) as max_count from (
SELECT Date_format(from_unixtime(unix_timestamp_column), '%Y-%m-%d') as date,
from_unixtime(unix_timestamp_column) as time,
count(*) as count
from your_table group by time ) a group by Date