CONCAT(DATE_FORMAT(sst.start_time '%Y-%m-%d'), '-', DATE_FORMAT(sst.end_time '%Y-%m-%d'))不对吗
时间: 2024-01-23 16:02:40 浏览: 177
不对,你的语法有一点错误。在 CONCAT 函数中,你需要使用逗号将多个参数分隔开。此外,你忘记了在日期格式化字符串中使用百分号来指示日期格式。正确的语法应该是:
CONCAT(DATE_FORMAT(sst.start_time, '%Y-%m-%d'), '-', DATE_FORMAT(sst.end_time, '%Y-%m-%d'))
相关问题
找出这段代码的问题(WITH C AS ( SELECT COUNT( * ) AA FROM wjj_yy_yldsj_gxysfjl A, wjj_yy_yldsj_grjkdaxx B WHERE A.HEALTH_ID = B.HEALTH_ID AND B.STATUS = '0' AND A.VISITS_DATE >= DATE_FORMAT( SYSDATE(), '%Y-%m-%d' ) ), D AS ( SELECT COUNT( * ) AA FROM wjj_yy_yldsj_gxysfjl A, wjj_yy_yldsj_grjkdaxx B WHERE A.HEALTH_ID = B.HEALTH_ID AND B.STATUS = '0' AND A.VISITS_DATE BETWEEN DATE_FORMAT(DATE_FORMAT( SYSDATE(), '%Y-%m-%d' ) - 1, '%Y-%m-%d' ) AND DATE_FORMAT(SYSDATE(), '%Y-%m-%d' ) ) SELECT ROUND((C.AA - D.AA )/D.AA,2)*100||'%' FROM C.AA,B.AA )
此段代码中存在以下问题:
1. 在 WITH 子句中定义的 C 和 D 是两个不同的子查询,但在最终的 SELECT 语句中只引用了 C.AA,缺少 D.AA 的引用。
2. 在最终的 SELECT 语句中使用了 || 连接符来连接计算结果和百分号,这是 Oracle 数据库中的写法,但在 MySQL 中应该使用 CONCAT 函数来连接字符串。
3. 子查询中使用了日期格式转换函数 DATE_FORMAT,但是这会导致查询性能下降,建议将日期格式转换提前到程序中,而不是在 SQL 查询中进行。
下面是修正后的代码(假设你想计算今天和昨天的数量差异百分比):
```
SELECT ROUND((C.AA - D.AA) / D.AA * 100, 2) AS percentage
FROM (
SELECT COUNT(*) AS AA
FROM wjj_yy_yldsj_gxysfjl A
JOIN wjj_yy_yldsj_grjkdaxx B ON A.HEALTH_ID = B.HEALTH_ID
WHERE B.STATUS = '0'
AND A.VISITS_DATE >= DATE(NOW())
) C, (
SELECT COUNT(*) AS AA
FROM wjj_yy_yldsj_gxysfjl A
JOIN wjj_yy_yldsj_grjkdaxx B ON A.HEALTH_ID = B.HEALTH_ID
WHERE B.STATUS = '0'
AND A.VISITS_DATE BETWEEN DATE(NOW() - INTERVAL 1 DAY) AND DATE(NOW())
) D;
```
优化这段sql SELECT (CASE WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '00:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '03:59:59' THEN '00:00-03:59' WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '04:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '07:59:59' THEN '04:00-07:59' WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '08:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '11:59:59' THEN '08:00-11:59' WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '12:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '15:59:59' THEN '12:00-15:59' WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '16:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '19:59:59' THEN '16:00-19:59' WHEN DATE_FORMAT( `start_time`, '%H:%i:%S' ) > '20:00:00' and DATE_FORMAT( `start_time`, '%H:%i:%S' ) <= '23:59:59' THEN '20:00-23:59' ELSE '' END) AS timeInterval, IFNULL( SUM( `dual_time` ), 0 ) AS totalTime, IFNULL( COUNT(1), 0 ) AS totalCall, IFNULL( SUM( CASE WHEN `local_action` = '主叫' THEN 1 ELSE 0 END ), 0 ) AS active, IFNULL( SUM( CASE WHEN `local_action` = '被叫' THEN 1 ELSE 0 END ), 0 ) AS passive FROM `dwd_t_call_record` WHERE `person_no` = '653129200105072624' GROUP BY HOUR ( `start_time` ) DIV 4
可以使用 MySQL 的 `CASE WHEN` 语句来简化这段 SQL 语句,同时避免重复的日期格式化操作。简化后的 SQL 语句如下:
```
SELECT
CONCAT(HOUR(`start_time`) DIV 4 * 4, ':00-', HOUR(`start_time`) DIV 4 * 4 + 3, ':59') AS timeInterval,
IFNULL(SUM(`dual_time`), 0) AS totalTime,
IFNULL(COUNT(1), 0) AS totalCall,
IFNULL(SUM(`local_action` = '主叫'), 0) AS active,
IFNULL(SUM(`local_action` = '被叫'), 0) AS passive
FROM
`dwd_t_call_record`
WHERE
`person_no` = '653129200105072624'
GROUP BY
HOUR(`start_time`) DIV 4
```
这个 SQL 语句使用了 `CONCAT` 函数来生成时间区间,使用了 `SUM` 函数的布尔表达式参数来计算主叫和被叫的数量。同时,将日期格式化操作转化为了整数运算,避免了多次重复的格式化操作。
阅读全文