选择每天最多sum()

时间:2013-10-04 08:11:01

标签: mysql sql

我有一个表记录UNIX时间具有Time字段的事务,因此每行都是一个事务。 现在我想找出一天中哪一秒有最大交易?

结果应为:

1-Oct-2013 20:02:34 45 ->(in day 1, at 20:02:34, we have 45 transaction for maximum, so on)
2-Oct-2013 12:34:21 99
3-Oct-2013 15:02:33 70

请提前帮助,谢谢。

5 个答案:

答案 0 :(得分:1)

一种方法

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 演示

注意:在此示例中,您有两个时间戳,这些时间戳在第一天具有相同的事务数最大值

答案 1 :(得分:0)

尝试以下查询

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

答案 2 :(得分:0)

  • 首先,获取独特的时间记录

SELECT DISTINCT your_time_column_name FROM table_name;

  • 然后循环以同时检查记录:

SELECT * FROM table_name WHERE your_time_column_name =“time”

  • 然后计算并找到最大值

答案 3 :(得分:0)

SELECT MAX(COUNT_NO), TIME_COLUMN FROM (SELECT COUNT(*) COUNT_NO ,TIME_COLUMN FROM     
TABLE_NAME
GROUP BY TIME_COLUMN)

试试这个。

答案 4 :(得分:0)

select yourtimecolumn,max(totalTrans) from (
select yourtimecolumn, count(id) as totalTrans
    from yourtable
    group by yourtimecolumn)a
group by day(yourtimecolumn)