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
时间: 2024-01-15 11:04:59 浏览: 119
这是一个SQL查询语句,用于从`ems_hour_allot`表中检索数据。查询结果将包括以下列:`username`、`userid`、`orgname`、`orgnameHr`、`company`、`idcard`、`aHour`、`bHour`、`cHour`、`dHour`、`eHour`、`fHour`、`countHour`和`yearMonth`。
该查询使用了多个聚合函数和条件语句。对于每个用户,它计算了不同类型的小时数的总和,并将其存储在相应的列中。最后,它根据给定条件对`yearMonth`进行分类,并返回结果。
请注意,查询中的日期条件是固定的,如果需要根据特定日期进行查询,请相应地修改条件。
相关问题
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语法有错误吗
根据提供的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`存在并且可以在查询中使用。如果该列不存在或者需要使用其他列,请相应地进行修改。
select a.orgId,a.orgName,a.Message,a.time,b.label,b.phone,b.contact from sms_log a where a.userName = '报警短信' and a.orgId = 6406 left join sms_log_ship b on a.id = b.log_id > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'left join sms_log_ship b on a.id = b.log_id' at line 1
你的查询语句有误,应该将 `left join` 放在 `where` 子句之后,正确的查询语句如下所示:
```
SELECT a.orgId, a.orgName, a.Message, a.time, b.label, b.phone, b.contact
FROM sms_log a
LEFT JOIN sms_log_ship b ON a.id = b.log_id
WHERE a.userName = '报警短信' AND a.orgId = 6406;
```
在这个查询语句中,我们使用了 `LEFT JOIN` 关键字将 `sms_log` 表和 `sms_log_ship` 表进行连接,连接条件是 `a.id = b.log_id`。然后,我们使用 `WHERE` 子句对查询结果进行过滤,只保留 `userName` 为 `'报警短信'` 且 `orgId` 为 `6406` 的记录。
注意,在 SQL 查询语句中,语句中的关键字和子句的顺序是有要求的,需要按照一定的顺序组合。
阅读全文