mysql group by 帮助

标签 mysql

经过多次尝试,我无法解决这个问题。 我有以下 mysql 查询:

SELECT
                  date_format(connect_time,"%Y-%m-%d %H") AS date,
                  NotConnected,
                  count(id) as calls,
                  sum(`duration`)/60 as minutes
   FROM test.`CDR_Vendors`,

   (SELECT        
       date_format(connect_time,"%Y-%m-%d %H") AS date,
       Count(id) as NotConnected
       FROM `CDR_Vendors_Failed`
       inner join Vendors on (CDR_Vendors_Failed.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors_Failed.i_customer = Customers.i_customer)
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date

   )Failed
       inner join Vendors on (CDR_Vendors.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date

结果:

date,Notconnected,calls, minutes
'2010-10-25 00', 408, 900, 6611.00
'2010-10-25 01', 408, 456, 2777.60
'2010-10-25 02', 408, 204, 1545.80
'2010-10-25 03', 408, 108, 1951.80
'2010-10-25 04', 408, 192, 895.60
'2010-10-25 05', 408, 300, 544.20
'2010-10-25 06', 408, 540, 961.20
'2010-10-25 07', 408, 1728, 5027.60
'2010-10-25 08', 408, 4968, 20986.40
'2010-10-25 09', 408, 7884, 33065.00
'2010-10-25 10', 408, 7836, 28182.20
'2010-10-25 11', 408, 1800, 3587.80

我注意到 Notconnected 字段的第一个值重复了。

请问我该如何解决?

最佳答案

这个查询会很慢。您在哪个表中有 connect_time 字段?如果在 CDR_Vendoes 和 CRD_Vendors_Failed 中,则不需要子查询。

也许您需要加入 CDR_Vendors_Failed 表?

SELECT
                  date_format(connect_time,"%Y-%m-%d %H") AS date,
                  Failed.NotConnected,
                  count(id) as calls,
                  sum(`duration`)/60 as minutes
   FROM test.`CDR_Vendors`
       inner join Vendors on (CDR_Vendors.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
       inner join
          (SELECT        
               date_format(connect_time,"%Y-%m-%d %H") AS date,
               Count(id) as NotConnected
               FROM `CDR_Vendors_Failed`
               inner join Vendors on (CDR_Vendors_Failed.i_vendor = Vendors.i_vendor)
               inner join Customers on (CDR_Vendors_Failed.i_customer = Customers.i_customer)
               WHERE
               Customers.name like "W%"
               and
               Vendors.name like "D%"
               and connect_time between curdate() and now()
               GROUP by date
               ORDER BY date
            )Failed on Failed.date = date_format(connect_time,"%Y-%m-%d %H")
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date

关于mysql group by 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4014014/

相关文章:

mysql - 检索表中不存在的行

php - 如何排除 MySQL 查询中的匹配记录?

php - 如何使用 Laravel 框架执行 MySQL 查询?

php - 如何在 synology 上执行带有扩展的 PHP CLI 脚本

php - 使用 php 出现 MySQL 错误

mysql - 从 MySQL 中删除重复记录

php - Web 应用程序 : Preprocess and send all items in one page load, 或将项目分成单独的客户端请求?

php - 在 Prestashop 后台订单表中添加状态字段

MySQL 错误 1241 (21000) : Operand should contain 1 column(s) on Aggregate Query

mysql - 使用 phpmyadmin 运行时插入查询语法错误