select ifnull(sum(case when TIMESTAMPDIFF(hour,rcsj,ccsj) <= 3 then 1 else 0 end),0) threelow, ifnull(sum(case when TIMESTAMPDIFF(hour,rcsj,ccsj) > 3 and TIMESTAMPDIFF(hour,rcsj,ccsj) <= 5 then 1 else 0 end),0) threeToFive, ifnull(sum(case when TIMESTAMPDIFF(hour,rcsj,ccsj) > 5 and TIMESTAMPDIFF(hour,rcsj,ccsj) <= 8 then 1 else 0 end),0) fiveToEight, ifnull(sum(case when TIMESTAMPDIFF(hour,rcsj,ccsj) > 8 then 1 else 0 end),0) eightup from vehicle_records where year(ccsj) = year(SYSDATE())
时间: 2024-04-14 14:31:20 浏览: 130
你的查询语句是正确的,它会从名为vehicle_records的表中选择数据,并根据条件计算四个不同时间范围内的记录数。这四个时间范围分别是:3小时以内(threelow)、3到5小时(threeToFive)、5到8小时(fiveToEight)和超过8小时(eightup)。查询还包括了一个条件,即ccsj列的年份与当前日期的年份相同。如果某个时间范围内没有记录,则对应的计数将返回0。
相关问题
Select(`ifnull(sum(CASE WHEN b.asid=a.link_id and a.user_type=? then b.sum_order_amt else 0 end),0) as sum_pay_amt, ifnull(sum(CASE WHEN b.asid=a.link_id and a.user_type=? then 1 else 0 end),0) as order_sum, ifnull(sum(CASE WHEN a.stmt_status=1 THEN a.profit ELSE 0 END), 0.00) as sum_profit, ifnull(sum(CASE WHEN a.stmt_status=0 and b.asid=a.link_id and a.user_type=? THEN a.pay_amt+b.sum_order_amt ELSE 0 END), 0.00) as ret_pay_amt`, model.USER_TYPE_SUPPLIER_AGENT, model.USER_TYPE_SUPPLIER_AGENT, model.USER_TYPE_SUPPLIER_AGENT).
这段代码是一个SQL查询语句,使用了`SELECT`关键字来选择多个字段。具体的查询内容如下:
- `ifnull(sum(CASE WHEN b.asid=a.link_id and a.user_type=? then b.sum_order_amt else 0 end),0) as sum_pay_amt`:计算满足条件的`b.sum_order_amt`的总和,如果没有满足条件的记录,则返回0。
- `ifnull(sum(CASE WHEN b.asid=a.link_id and a.user_type=? then 1 else 0 end),0) as order_sum`:计算满足条件的记录数,如果没有满足条件的记录,则返回0。
- `ifnull(sum(CASE WHEN a.stmt_status=1 THEN a.profit ELSE 0 END), 0.00) as sum_profit`:计算`a.profit`字段的总和,但仅针对`a.stmt_status`等于1的记录进行计算,如果没有满足条件的记录,则返回0.00。
- `ifnull(sum(CASE WHEN a.stmt_status=0 and b.asid=a.link_id and a.user_type=? THEN a.pay_amt+b.sum_order_amt ELSE 0 END), 0.00) as ret_pay_amt`:计算满足条件的`a.pay_amt+b.sum_order_amt`字段的总和,但仅针对`a.stmt_status`等于0、`b.asid=a.link_id`和`a.user_type=?`的记录进行计算,如果没有满足条件的记录,则返回0.00。
这些查询字段将作为结果的列,并分别命名为`sum_pay_amt`、`order_sum`、`sum_profit`和`ret_pay_amt`。其中,`?`是一个占位符,用于将`model.USER_TYPE_SUPPLIER_AGENT`的值填充到查询中。
SELECT ak.dept_id deptId,ak.dept_name deptName,ak.parent_id parentId, IFNULL(pk.primaryShould,0) primaryShould,IFNULL(pk.primaryNot,0) primaryNot,IFNULL(pk.primaryGraduated,0) primaryGraduated, IFNULL(mk.middleShould,0) middleShould,IFNULL(mk.middleNot,0) middleNot,IFNULL(mk.middleGraduated,0) middleGraduated, IFNULL(hk.highShould,0) highShould,IFNULL(hk.highGraduated,0) highGraduated,IFNULL(hk.highNot,0) highNot FROM sys_dept ak LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 6 THEN 1 ELSE 0 END) primaryShould, SUM(CASE WHEN grade = 6 AND is_graduation = 'N' THEN 1 ELSE 0 END) primaryNot, SUM(CASE WHEN grade = 6 AND is_graduation = 'Y' THEN 1 ELSE 0 END) primaryGraduated FROM primary_school_roll WHERE primary_student_id LIKE concat('20', '%') GROUP BY subordinate_school) pk ON ak.dept_id = pk.subordinate_school LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 9 THEN 1 ELSE 0 END) middleShould, SUM(CASE WHEN grade = 9 AND is_graduation = 'N' THEN 1 ELSE 0 END) middleNot, SUM(CASE WHEN grade = 9 AND is_graduation = 'Y' THEN 1 ELSE 0 END) middleGraduated FROM middle_school_roll WHERE middle_student_id LIKE concat('20', '%') GROUP BY subordinate_school) mk ON ak.dept_id = mk.subordinate_school LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 12 THEN 1 ELSE 0 END) highShould, SUM(CASE WHEN graduation_way = '001' AND is_graduation = 'T' THEN 1 ELSE 0 END) highNot, SUM(CASE WHEN graduation_way = '000' AND is_graduation = 'Y' THEN 1 ELSE 0 END) highGraduated FROM high_school_roll WHERE high_student_id LIKE concat('20', '%') GROUP BY subordinate_school) hk ON ak.dept_id = hk.subordinate_school WHERE ak.state = 3 GROUP BY ak.dept_id
这是一条 SQL 查询语句,根据部门ID将三个子查询的结果进行合并,最终返回各部门的信息以及不同年级的学生人数及毕业情况。
具体实现过程是,使用 LEFT JOIN 连接 sys_dept 表和三个子查询的结果集,根据部门ID进行关联。在子查询中,使用 SUM() 函数对不同年级的学生人数进行求和,然后根据不同的条件进行分类统计。最后,使用 GROUP BY 对查询结果按部门ID进行分组,以得到每个部门的统计结果。
需要注意的是,这条查询语句中使用了 IFNULL() 函数对查询结果进行处理,以避免在统计结果为 NULL 时出现错误。同时,也需要注意在使用 LIKE 进行模糊查询时,应该使用索引来提高查询效率。
阅读全文