SELECT a.username, a.userid, a.orgname, a.orgnamehr orgnameHr, a.company company, a.idcard, sum(CASE a.hour_type WHEN 'A' THEN a.allot_hour ELSE 0 END ) aHour, sum(CASE a.hour_type WHEN 'B' THEN a.allot_hour ELSE 0 END ) bHour, sum(CASE a.hour_type WHEN 'C' THEN a.allot_hour ELSE 0 END ) cHour, sum(CASE a.hour_type WHEN 'D' THEN a.allot_hour ELSE 0 END ) dHour, sum(CASE a.hour_type WHEN 'E' THEN a.allot_hour ELSE 0 END ) eHour, sum(CASE a.hour_type WHEN 'F' THEN a.allot_hour ELSE 0 END ) fHour, sum(CASE WHEN a.hour_type IN ('A','B','C','D','E','F') THEN a.allot_hour ELSE 0 END ) countHour, CASE yearMonth WHEN CONVERT(varchar(10), d.break_sure_date, 23) >= CONVERT(varchar(10),'2023-06-06', 23) and CONVERT(varchar(10), d.break_sure_date, 23) < CONVERT(varchar(10),'2023-07-06', 23) THEN '1' ELSE '2' END FROM ems_hour_allot a 这个sql语法有错误吗
时间: 2024-01-15 17:04:41 浏览: 65
权限管理系统数据字典.doc
根据提供的SQL查询,有一个语法错误。在查询的末尾缺少一个`GROUP BY`子句,以指定如何对结果进行分组。根据查询中的列,你可能需要在`GROUP BY`子句中包含以下列:`a.username`、`a.userid`、`a.orgname`、`a.orgnamehr`、`a.company`、`a.idcard`和`yearMonth`。
以下是修正后的查询:
```sql
SELECT
a.username,
a.userid,
a.orgname,
a.orgnamehr AS orgnameHr,
a.company,
a.idcard,
SUM(CASE a.hour_type WHEN 'A' THEN a.allot_hour ELSE 0 END) AS aHour,
SUM(CASE a.hour_type WHEN 'B' THEN a.allot_hour ELSE 0 END) AS bHour,
SUM(CASE a.hour_type WHEN 'C' THEN a.allot_hour ELSE 0 END) AS cHour,
SUM(CASE a.hour_type WHEN 'D' THEN a.allot_hour ELSE 0 END) AS dHour,
SUM(CASE a.hour_type WHEN 'E' THEN a.allot_hour ELSE 0 END) AS eHour,
SUM(CASE a.hour_type WHEN 'F' THEN a.allot_hour ELSE 0 END) AS fHour,
SUM(CASE WHEN a.hour_type IN ('A','B','C','D','E','F') THEN a.allot_hour ELSE 0 END) AS countHour,
CASE
WHEN CONVERT(varchar(10), d.break_sure_date, 23) >= CONVERT(varchar(10),'2023-06-06', 23) AND CONVERT(varchar(10), d.break_sure_date, 23) < CONVERT(varchar(10),'2023-07-06', 23) THEN '1'
ELSE '2'
END AS yearMonth
FROM
ems_hour_allot a
GROUP BY
a.username,
a.userid,
a.orgname,
a.orgnamehr,
a.company,
a.idcard,
yearMonth;
```
请注意,此修正假设`ems_hour_allot`表中的列`d.break_sure_date`存在并且可以在查询中使用。如果该列不存在或者需要使用其他列,请相应地进行修改。
阅读全文