按帮助分组(按多个分组,有重复)

时间:2017-04-17 13:42:15

标签: mysql date time group-by grouping

我有一项任务,我需要按日期和Provider_name对结果进行分组,但目前我的代码列出了多个日期和提供商。 (每天需要一个提供商(总共25天)所以我的表格显示了当天提供商收到的消息数量以及他们赚取了多少消息)

这需要是我的结果。 Result table

But this is what i'm currently getting

这是我目前的代码

SELECT date_format( time, '%Y-%m-%d' ) AS Date, provider_name, COUNT( message_id ) AS Messages_count, SUM( price ) AS Total_price
FROM mobile_log_messages_sms
INNER JOIN service_instances ON service_instances.service_instance_id =   mobile_log_messages_sms.service_instance_id
INNER JOIN mobile_providers ON mobile_providers.network_code = mobile_log_messages_sms.network_code
WHERE time
BETWEEN '2017-02-26 00:00:00'
AND time
AND '2017-03-22 00:00:00'
AND price IS NOT NULL
AND price <> ''
AND service IS NOT NULL
AND service <> ''
AND enabled IS NOT NULL
AND enabled >=1
GROUP BY provider_name, time
ORDER BY time DESC

你能告诉我我搞砸了哪里,我真的无法找到答案。

2 个答案:

答案 0 :(得分:1)

试试这样:

    ....

    GROUP BY provider_name, date_format( time, '%Y-%m-%d' )
    ORDER BY time DESC

您正在按时间对结果进行分组,包括小时,分钟和秒等...这就是您从同一天获得不同计数的原因。请尝试按日分组。

答案 1 :(得分:0)

tp1 = TracePoint.new do |tp| if tp.event == :call method = tp.defined_class.method(tp.method_id) arguments = method.parameters.map do |param| "#{param[1]}: #{tp.binding.local_variable_get(param[1])}" end.join(", ") puts "Starting #{tp.method_id} with arguments #{arguments}" elsif tp.event.to_s == "return" puts "Finished #{tp.method_id} with result: #{tp.return_value}" end end tp1.enable def my_method1(a, b) a + b end puts my_method1(2, 3) 列为time。所以它按日期和时间分组,而不仅仅是日期。

将GROUP BY语句更改为

datetime