【问题标题】:Group by Highest Total Month按最高总月份分组
【发布时间】:2012-11-23 13:41:45
【问题描述】:

我希望按模块名称(目前是这样)订购下面的 MySQL 语句,但仅包括日期范围内包含的所有 module_types 的 COUNT 排名前 50 的模块。关于我将如何去做的任何想法? IE。如果一个模块的 1 月、2 月、3 月的所有“计数”总数高于另一个模块的相同,则该模块的所有三个月都将高于其他记录。

我试图嵌入一个带有总数的选择,而按此排序似乎需要大量时间。

SELECT
  DATE_FORMAT(tbl_client_modules.c_module_month, '%b %y') AS MONTH, 
  tbl_modules.module_name,
  count(tbl_client_modules.c_module_type),
  tbl_client_modules.c_module_type AS MOD_TYPE 
FROM 
  tbl_client_details 
  LEFT OUTER JOIN tbl_client_status ON tbl_client_details.cd_status = tbl_client_status.cl_status_id 
  LEFT OUTER JOIN tbl_client_modules ON tbl_client_details.cd_ZBI_no = tbl_client_modules.cl_module_zbi AND tbl_client_details.cd_UCN = tbl_client_modules.c_module_UCN AND tbl_client_details.cd_co_code = tbl_client_modules.c_module_co_code 
  LEFT OUTER JOIN tbl_modules ON tbl_client_modules.c_module_type = tbl_modules.module_id 
WHERE
  tbl_client_details.cd_removed_date is null
  AND TRIM(tbl_client_details.cd_client_name) <> '' 
  AND (tbl_client_details.cd_region = 'WC')
  AND (tbl_client_modules.c_module_month >= '2012-07-01' AND tbl_client_modules.c_module_month <= '2012-10-30')
  AND tbl_client_modules.c_module_vol > 0 
GROUP BY tbl_client_modules.c_module_month, tbl_client_modules.c_module_type 
ORDER BY tbl_modules.module_name;

索引:

tbl_client_details, 0, PRIMARY,        1, cd_id, A, , , , , BTREE, , 
tbl_client_details, 0, PRIMARY,        2, cd_ucn, A, , , , , BTREE, , 
tbl_client_details, 0, PRIMARY,        3, cd_ZBI_no, A, 55351, , , , BTREE, , 
tbl_client_details, 1, cd_ucn,         1, cd_ucn, A, 55351, , , , BTREE, , 
tbl_client_details, 1, cd_opm,         1, cd_OPM, A, 321, , , YES, BTREE, , 
tbl_client_details, 1, cd_zbi_no,      1, cd_ZBI_no, A, 55351, , , , BTREE, , 
tbl_client_details, 1, cd_cpe_rm_code, 1, cd_cpe_rm_code, A, 1729, , , YES, BTREE, , 
tbl_client_details, 1, cd_sic_code,    1, cd_sic_code, A, 802, , , YES, BTREE, , 
tbl_client_details, 1, cd_enrol_date,  1, cd_enrol_date, A, 13837, , , YES, BTREE, , 
tbl_client_details, 1, cd_co_code,     1, cd_co_code, A, 8, , , YES, BTREE, , 
tbl_client_details, 1, cd_client_name, 1, cd_client_name, A, 55351, , , YES, BTREE, , 
tbl_client_details, 1, cd_segment,     1, cd_segment, A, 36, , , , BTREE, , 
tbl_client_details, 1, cd_region,      1, cd_region, A, 18, , , YES, BTREE, , 
tbl_client_details, 1, cd_status,      1, cd_status, A, 295, , , YES, BTREE, ,  

tbl_client_status,  0, PRIMARY,        1, cl_status_id, A, 32, , , , BTREE, , 
tbl_client_status,  1, cl_status_id,   1, cl_status_id, A, 32, , , , BTREE, , 

tbl_client_modules, 0, PRIMARY,          1, cl_module_id, A, 12135739, , , , BTREE, , 
tbl_client_modules, 1, cl_module_zbi,    1, cl_module_zbi, A, 53697, , , , BTREE, , 
tbl_client_modules, 1, c_module_type,    1, c_module_type, A, 104, , , , BTREE, , 
tbl_client_modules, 1, c_module_month,   1, c_module_month, A, 27, , , YES, BTREE, , 
tbl_client_modules, 1, c_module_ucn,     1, c_module_ucn, A, 63872, , , YES, BTREE, , 
tbl_client_modules, 1, c_module_co_code, 1, c_module_co_code, A, 9, , , YES, BTREE, , 
tbl_client_modules, 1, c_module_vol,     1, c_module_vol, A, 32020, , , YES, BTREE, , 

tbl_modules,        0, PRIMARY,        1, module_id, A, 109, , , , BTREE, , 
tbl_modules,        1, module_id,      1, module_id, A, 109, , , , BTREE, , 
tbl_modules,        1, module_tab,     1, module_tab, A, 9, , , YES, BTREE, , 

【问题讨论】:

  • 尝试将您的 GROUP BY 更改为 GROUP BY DATE_FORMAT(c_module_month, '%b %y'), module_name, c_module_type。 MySQL 在其 GROUP BY 设置中有一个可怕的错误。
  • 另外,很难判断这个查询发生了什么,因为你没有用表名来限定你的字段名。你的索引在哪里?此外,如果您的列 c_module_month 仅包含引用月份的第一天的值,您可以通过摆脱 DATE_FORMAT 调用来加快速度。
  • 感谢合格的字段名称和显示的索引。我将根据建议重试该组。我之前确实尝试过,但无法正常工作。

标签: mysql rollup


【解决方案1】:

我发现“WITH ROLLUP”功能可以满足我的需要。下一个问题是找到一种对此进行排序的方法。在其中工作,这里有一个挑战的答案:sort the "rollup" in group by

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多