0

我希望通过模块名称(目前是这样)订购下面的 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, , 
4

1 回答 1

0

我发现“WITH ROLLUP”功能正在做我需要的事情。下一个问题是找到一种对此进行排序的方法。在其中工作,这里有一个挑战的答案:对组中的“汇总”进行排序

于 2012-11-26T10:14:06.500 回答