优化这段:SQL:SELECT CONCAT( HOUR ( mail_send_time ) DIV 1 * 1, ':00-', HOUR ( mail_send_time ) DIV 1 * 1, ':59' ) AS timeInterval, IFNULL( COUNT( * ), 0 ) AS total, IFNULL( SUM( contact_account_type = '微信' ), 0 ) AS wxTotal, IFNULL( SUM( contact_account_type = 'QQ' ), 0 ) AS qqTotal, IFNULL( SUM( local_action = '发送' AND contact_account_type = '微信' ), 0 ) AS wxSend, IFNULL( SUM( local_action = '接收' AND contact_account_type = '微信'), 0 ) AS wxReceive , IFNULL( SUM( local_action = '发送' AND contact_account_type = 'QQ'), 0 ) AS qqSend, IFNULL( SUM( local_action = '接收' AND contact_account_type = 'QQ'), 0 ) AS qqReceive FROM dwd_t_friend_talk WHERE person_no = '653129200105072624' AND contact_account_type IN ( '微信', 'QQ' ) AND friend_account <> '' AND contact_account_type <> '' AND mail_send_time IS NOT NULL AND mail_send_time <> '' GROUP BY HOUR ( mail_send_time ) DIV 1;其中的contact_account_type和local_action不用汉子,用码值代替,现在有一张dwd_t_code_value表存储码值和中文描述,改造这条SQL,将汉字替换为码值
时间: 2024-02-14 18:13:14 浏览: 128
优化后的SQL语句如下:
```
SELECT
CONCAT(HOUR(mail_send_time) DIV 1 * 1, ':00-', HOUR(mail_send_time) DIV 1 * 1, ':59') AS timeInterval,
IFNULL(COUNT(*), 0) AS total,
IFNULL(SUM(contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='微信')), 0) AS wxTotal,
IFNULL(SUM(contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='QQ')), 0) AS qqTotal,
IFNULL(SUM(local_action = (SELECT code_value FROM dwd_t_code_value WHERE code_type='local_action' AND name='发送') AND contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='微信')), 0) AS wxSend,
IFNULL(SUM(local_action = (SELECT code_value FROM dwd_t_code_value WHERE code_type='local_action' AND name='接收') AND contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='微信')), 0) AS wxReceive,
IFNULL(SUM(local_action = (SELECT code_value FROM dwd_t_code_value WHERE code_type='local_action' AND name='发送') AND contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='QQ')), 0) AS qqSend,
IFNULL(SUM(local_action = (SELECT code_value FROM dwd_t_code_value WHERE code_type='local_action' AND name='接收') AND contact_account_type = (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name='QQ')), 0) AS qqReceive
FROM
dwd_t_friend_talk
WHERE
person_no = '653129200105072624'
AND contact_account_type IN (SELECT code_value FROM dwd_t_code_value WHERE code_type='contact_type' AND name IN ('微信', 'QQ'))
AND friend_account <> ''
AND contact_account_type <> ''
AND mail_send_time IS NOT NULL
AND mail_send_time <> ''
GROUP BY
HOUR(mail_send_time) DIV 1;
```
改造后的SQL语句使用了子查询,通过dwd_t_code_value表获取contact_account_type和local_action字段的码值,从而将原本的汉字替换为了码值。
阅读全文