case when end_time>start_time then end_time else start_time+1 end
时间: 2024-03-29 12:38:53 浏览: 127
这是一个SQL语句中的条件表达式,它的作用是用来判断时间段是否合法。具体来说,如果结束时间大于开始时间,则返回结束时间;否则,返回开始时间加上1。这个表达式可以在查询语句中使用,例如:
SELECT column1, column2, case when end_time>start_time then end_time else start_time+1 end as valid_time FROM table_name;
其中,column1和column2是需要查询的列,valid_time是根据条件表达式计算出来的列。
相关问题
帮我优化postgresql语句,如下:select source_name as "SOURCE_NAME",type_name as "TYPE_NAME",shift_date as "SHIFT_DATE",dd as "DD",task_title as "TASK_TITLE", task_content as "TASK_CONTENT",task_creator as "TASK_CREATOR",task_executor as "TASK_EXECUTOR",task_description as "TASK_DESCRIPTION", create_time as "CREATE_TIME",creatorid as "CREATORID",creatorname as "CREATORNAME",org_id as "ORG_ID",executorid as "EXECUTORID",executorname as "EXECUTORNAME", plan_start_time as "PLAN_START_TIME",plan_end_time as "PLAN_END_TIME",act_start_time as "ACT_SART_TIME",act_end_time as "ACT_END_TIME", gap_date as "GAP_DATE",task_status as "TASK_STATUS",1 as "TASK_QTY", (case when task_status='Finish' then '已结案' when task_status='Confirm'then '已结案' when gap_date>0 then '已逾期' --直播状态如下 --when gap_date>0 and gap_date<=1 then '已逾期' when gap_date>0.3 then '已结案' when gap_date<=0 and task_status='Going' then '进行中' when gap_date<=0 and task_status='Plan' then '计划中' end ) as "STATUS" -------union from ((select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - plan_end_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.r_est_task WHERE SITE = 'S01' --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) union (select source_name,source_id,type_name,task_id,to_char(shift_date,'MM')||'月' as shift_date,task_title,task_content,task_status,task_creator, Plan_Start_Time,plan_end_time,act_start_time,(case when act_end_time is null then current_date else act_end_time end) as act_end_time, create_time,SUBSTR(TASK_EXECUTOR,1,8)AS TASK_EXECUTOR,'M'||TO_CHAR(SHIFT_DATE,'MM') as dd, round(date_part('epoch', (case when act_end_time is null then now() else act_end_time end) - create_time))/60/60/24 as gap_date, TASK_DESCRIPTION from estone.h_Est_Comp WHERE SITE = 'S01' and substr(pt_mfg_date,1,6)>=to_char(current_date-100,'yyyymm') --and to_number(substr(pt_mfg_date,5,2),'99G999D')>=extract(month from current_date)-3 --and to_char(shift_date,'yyyy')=to_char(current_date,'yyyy') --and extract(month from shift_date)>extract(month from current_date)-3 and shift_Date>to_date('20221031','yyyymmdd') ) )xx left join (select emp_no as CreatorID,emp_name as CreatorName from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01')yy on xx.task_creator = yy.CreatorID left join (select emp_no as ExecutorId,emp_name as ExecutorName,org_id from restricted.ausref_emp_data_ausz where substr(org_id,1,4)='MS01' )aa on xx.task_executor = aa.ExecutorId
可以尝试以下优化:
1. 将 SELECT 语句中的多个子查询转换为 JOIN,这样可以减少子查询的次数,提高查询效率。
2. 对于 WHERE 子句中的一些判断条件,可以根据具体情况添加索引,加快查询速度。
3. 对于计算字段 "STATUS" 中的逻辑判断,可以使用 CASE WHEN THEN ELSE END 语句,这样可以使查询更加简洁明了。
下面是优化后的 SQL 语句:
SELECT
source_name AS "SOURCE_NAME",
type_name AS "TYPE_NAME",
shift_date AS "SHIFT_DATE",
dd AS "DD",
task_title AS "TASK_TITLE",
task_content AS "TASK_CONTENT",
task_creator AS "TASK_CREATOR",
task_executor AS "TASK_EXECUTOR",
task_description AS "TASK_DESCRIPTION",
create_time AS "CREATE_TIME",
creatorid AS "CREATORID",
creatorname AS "CREATORNAME",
org_id AS "ORG_ID",
executorid AS "EXECUTORID",
executorname AS "EXECUTORNAME",
plan_start_time AS "PLAN_START_TIME",
plan_end_time AS "PLAN_END_TIME",
act_start_time AS "ACT_SART_TIME",
act_end_time AS "ACT_END_TIME",
gap_date AS "GAP_DATE",
task_status AS "TASK_STATUS",
1 AS "TASK_QTY",
CASE
WHEN task_status IN ('Finish', 'Confirm') THEN '已结案'
WHEN gap_date > 0.3 THEN '已结案'
WHEN gap_date > 0 THEN '已逾期'
WHEN gap_date <= 0 AND task_status = 'Going' THEN '进行中'
WHEN gap_date <= 0 AND task_status = 'Plan' THEN '计划中'
ELSE NULL
END AS "STATUS"
FROM (
SELECT
source_name,
source_id,
type_name,
task_id,
to_char(shift_date, 'MM') || '月' AS shift_date,
task_title,
task_content,
task_status,
task_creator,
Plan_Start_Time,
plan_end_time,
act_start_time,
(CASE WHEN act_end_time IS NULL THEN current_date ELSE act_end_time END) AS act_end_time,
create_time,
SUBSTR(TASK_EXECUTOR, 1, 8) AS TASK_EXECUTOR,
'M' || TO_CHAR(SHIFT_DATE, 'MM') AS dd,
ROUND(DATE_PART('epoch', (CASE WHEN act_end_time IS NULL THEN now() ELSE act_end_time END) - plan_end_time)) / 60 / 60 / 24 AS gap_date,
TASK_DESCRIPTION
FROM estone.r_est_task t
JOIN restricted.ausref_emp_data_ausz c ON t.task_creator = c.emp_no
JOIN restricted.ausref_emp_data_ausz e ON t.task_executor = e.emp_no
WHERE
t.SITE = 'S01'
AND substr(c.org_id, 1, 4) = 'MS01'
AND substr(e.org_id, 1, 4) = 'MS01'
AND t.shift_date > to_date('20221031', 'yyyymmdd')
AND t.shift_date >= current_date - interval '3 month'
) UNION (
SELECT
source_name,
source_id,
type_name,
task_id,
to_char(shift_date, 'MM') || '月' AS shift_date,
task_title,
task_content,
task_status,
task_creator,
Plan_Start_Time,
plan_end_time,
act_start_time,
(CASE WHEN act_end_time IS NULL THEN current_date ELSE act_end_time END) AS act_end_time,
create_time,
SUBSTR(TASK_EXECUTOR, 1, 8) AS TASK_EXECUTOR,
'M' || TO_CHAR(SHIFT_DATE, 'MM') AS dd,
ROUND(DATE_PART('epoch', (CASE WHEN act_end_time IS NULL THEN now() ELSE act_end_time END) - create_time)) / 60 / 60 / 24 AS gap_date,
TASK_DESCRIPTION
FROM estone.h_Est_Comp c
JOIN restricted.ausref_emp_data_ausz e ON c.task_executor = e.emp_no
WHERE
c.SITE = 'S01'
AND substr(e.org_id, 1, 4) = 'MS01'
AND substr(c.pt_mfg_date, 1, 6) >= to_char(current_date - interval '100 day', 'yyyymm')
AND c.shift_date > to_date('20221031', 'yyyymmdd')
) AS xx
LEFT JOIN restricted.ausref_emp_data_ausz AS yy ON xx.task_creator = yy.emp_no
LEFT JOIN restricted.ausref_emp_data_ausz AS aa ON xx.task_executor = aa.emp_no
SELECT SUM( CASE WHEN A.RUNLONGTIME IS NULL THEN 0 ELSE TO_NUMBER( A.RUNLONGTIME ) END ) AS 运行时间, SUM( CASE WHEN A.TOTALTIME IS NULL THEN 0 ELSE 12 END ) AS 总时间, CONCAT( CONCAT( B.SUPUSER_ID, ':' ), B.SUPUSER_NAME ) AS 技术员, ROUND(( SUM( CASE WHEN A.RUNLONGTIME IS NULL THEN 0 ELSE TO_NUMBER( A.RUNLONGTIME ) END ) / SUM( CASE WHEN A.TOTALTIME IS NULL THEN 0 ELSE 12 END )), 4 ) * 100 oee , '2023-06-26 08:00:00' AS 开始时间, '2023-06-27 08:00:00' AS 结束时间, ( CASE C.PROCESS WHEN '3' THEN '焊线A区' WHEN '5' THEN '焊线B区' WHEN '7' THEN '焊线C区' END ) AS 区域 FROM RPT_EQP_STATETIME A LEFT JOIN BAS_USEREQP_CONFIG B ON A.EQP_ID = B.EQP_ID AND A.CREATED_TIME = B.END_TIME LEFT JOIN BAS_EQP_EQUIPMENT C ON A.EQP_ID = C.EQP_ID WHERE A.EQP_ID LIKE 'WB%' AND A.CREATED_TIME > to_date( '2023-06-16 08:00:00', 'yyyy-MM-dd hh24:mi:ss' ) AND A.CREATED_TIME <= to_date( '2023-06-26 20:00:00', 'yyyy-MM-dd hh24:mi:ss' ) AND B.START_TIME >= TO_DATE( '2023-06-16 08:00:00', 'YYYY-MM-DD HH24:MI:SS' ) AND B.END_TIME <= TO_DATE( '2023-06-26 20:00:00', 'YYYY-MM-DD HH24:MI:SS' ) AND A.CREATED_TIME = B.END_TIME GROUP BY B.SUPUSER_ID, B.SUPUSER_NAME, C.PROCESS ORDER BY OEE DESC
这是一个复杂的SELECT语句,它使用了多个表的JOIN和聚合函数来生成结果集。以下是对该语句的解释:
```sql
SELECT
SUM( CASE WHEN A.RUNLONGTIME IS NULL THEN 0 ELSE TO_NUMBER( A.RUNLONGTIME ) END ) AS 运行时间,
SUM( CASE WHEN A.TOTALTIME IS NULL THEN 0 ELSE 12 END ) AS 总时间,
CONCAT( CONCAT( B.SUPUSER_ID, ':' ), B.SUPUSER_NAME ) AS 技术员,
ROUND((
SUM( CASE WHEN A.RUNLONGTIME IS NULL THEN 0 ELSE TO_NUMBER( A.RUNLONGTIME ) END ) / SUM( CASE WHEN A.TOTALTIME IS NULL THEN 0 ELSE 12 END )),
4
) * 100 oee ,
'2023-06-26 08:00:00' AS 开始时间,
'2023-06-27 08:00:00' AS 结束时间,
( CASE C.PROCESS WHEN '3' THEN '焊线A区' WHEN '5' THEN '焊线B区' WHEN '7' THEN '焊线C区' END ) AS 区域
FROM
RPT_EQP_STATETIME A
LEFT JOIN BAS_USEREQP_CONFIG B ON A.EQP_ID = B.EQP_ID
AND A.CREATED_TIME = B.END_TIME
LEFT JOIN BAS_EQP_EQUIPMENT C ON A.EQP_ID = C.EQP_ID
WHERE
A.EQP_ID LIKE 'WB%'
AND A.CREATED_TIME > to_date( '2023-06-16 08:00:00', 'yyyy-MM-dd hh24:mi:ss' )
AND A.CREATED_TIME <= to_date( '2023-06-26 20:00:00', 'yyyy-MM-dd hh24:mi:ss' )
AND B.START_TIME >= TO_DATE( '2023-06-16 08:00:00', 'YYYY-MM-DD HH24:MI:SS' )
AND B.END_TIME <= TO_DATE( '2023-06-26 20:00:00', 'YYYY-MM-DD HH24:MI:SS' )
AND A.CREATED_TIME = B.END_TIME
GROUP BY
B.SUPUSER_ID,
B.SUPUSER_NAME,
C.PROCESS
ORDER BY
OEE DESC
```
这个查询语句从表RPT_EQP_STATETIME(A)、BAS_USEREQP_CONFIG(B)和BAS_EQP_EQUIPMENT(C)中检索数据。它使用了LEFT JOIN关键字将这些表连接起来。
查询的目的是计算运行时间、总时间、技术员、OEE指数、开始时间、结束时间和区域。其中,运行时间和总时间是通过SUM函数和CASE表达式计算得出的。技术员是通过CONCAT函数将SUPUSER_ID和SUPUSER_NAME拼接而成的。OEE指数是通过运行时间除以总时间,并乘以100计算得出的。
查询还包括了一些筛选条件,例如EQP_ID的模糊匹配,CREATED_TIME的范围限制等。
最后,查询使用GROUP BY子句对SUPUSER_ID、SUPUSER_NAME和PROCESS进行分组,并使用ORDER BY子句按OEE指数降序排序结果集。
阅读全文