case when LOCATE then用法
时间: 2023-11-23 16:55:59 浏览: 113
`CASE WHEN`和`LOCATE`函数在SQL中的用法如下:
`LOCATE`函数用于在字符串中查找子字符串,并返回子字符串第一次出现的位置。它的语法如下:
```sql
LOCATE(substr,str,pos)
```
其中,`substr`是要查找的子字符串,`str`是要在其中查找的字符串,`pos`是可选参数,表示从哪个位置开始查找,默认为1。
`CASE WHEN`函数用于根据条件返回不同的结果。它的语法如下:
```sql
CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
```
其中,`condition`是要判断的条件,`result`是满足条件时返回的结果。
结合起来,`CASE WHEN`和`LOCATE`函数可以用于在SQL中查找字符串中是否包含某个子字符串,并根据结果返回不同的值。例如:
```sql
SELECT CASE WHEN LOCATE('apple', fruit) > 0 THEN '包含' ELSE '不包含' END AS result
FROM fruits;
```
这个查询会在`fruits`表中查找`fruit`列中是否包含`apple`子字符串,如果包含则返回`包含`,否则返回`不包含`。
相关问题
select lot_hs.lot_id as lot_id,lot_type,lot_hs.mainpd_id, created_time,COMPLETE_TIME, value(bank.banktime,0) as banktime , round ( ( 1.00*(days(COMPLETE_TIME)-days(created_time)) + (hour(COMPLETE_TIME)-hour(created_time))*1.00/24 + (minute(COMPLETE_TIME)-minute(created_time))*1.00/24/60 + (second(COMPLETE_TIME)-second(created_time))1.00/24/60/60) - value(bank.banktime,0),3) as use_days, customer_id, coalesce(cc.cust_id_define,lot_hs.customer_id) as cust_id2, cc.cycletime_target as ct_target, date,layer, round(count() over(partition by coalesce(cc.cust_id_define,lot_hs.customer_id),cc.cycletime_target)*0.9,0) cnt, row_number() over(partition by coalesce(cc.cust_id_define,lot_hs.customer_id),cc.cycletime_target order by ( ( days(COMPLETE_TIME)-days(created_time) + (hour(COMPLETE_TIME)-hour(created_time))*1.00/24 + (minute(COMPLETE_TIME)-minute(created_time))*1.00/24/60 + (second(COMPLETE_TIME)-second(created_time))*1.00/24/60/60) - value(bank.banktime,0))/layer) id From (select date(a.claim_time) as date, a.lot_id, a.lot_type,a.mainpd_id,a.prodspec_id,a.custprod_id, case when(date(b.created_time) <= '2009-01-05') then b.created_time + 21 days else b.created_time end as created_time, CASE WHEN A.CUST_id in ('MCA','NPA','SET') THEN a.COMPLETE_TIME ELSE a.COMPLETE_TIME END COMPLETE_TIME, a.cust_id as customer_id, a.ope_category, c.layer From f3rpt.F3_TB_DAILY_FABOUT a, f3rpt.fvlot b, (select mainpd_id, sum(masks)layer from f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL group by mainpd_id) as c, (select * from (select lot_id, max(claim_time)claim_time, count(case when(ope_category='Ship')then lot_id else null end) as LS, count(case when(ope_category='Unship') then lot_id else null end) as LUS from f3rpt.F3_TB_DAILY_FABOUT where substr(lot_id,1,2) not in('CA','CW','ES','E0','EM') and lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category in ('Ship','Unship') and year(claim_time) = year(current date - 1 days) and month(claim_time) = month(current date - 1 days) group by lot_id) as a where LS - LUS > 0 ) as lot Where a.lot_id = b.lot_id and b.mainpd_id = c.mainpd_id and a.lot_id = lot.LOT_ID and a.claim_time = lot.claim_time and a.ope_category = 'Ship' and a.cust_id in ('SM','BOE','GSC','NPA','GTA') ) as lot_hs left outer join (select lot_id,max(bankin_time) banktime from f3rpt.asmc_dpm where bankin_time>0 group by lot_id) bank on (lot_hs.lot_id = bank.lot_id) left join f3cim.f3cim_cfg_cust_rule cc on case when lot_hs.customer_id='WXM' THEN 'WII'||SUBSTR(lot_hs.mainpd_id,6,1) else lot_hs.customer_id end = cc.cust_id and locate(cc.mainpd_id,lot_hs.mainpd_id)>0 and locate(cc.prodspec_id,lot_hs.prodspec_id)>0 and locate(cc.custprod_id_45,substr(lot_hs.custprod_id,3,3))>0 where lot_hs.ope_category = 'Ship' ;以上sql如何优化
这是一个比较复杂的 SQL 语句,可以从以下几个方面考虑进行优化:
1.使用 JOIN 替代子查询
可以使用 JOIN 语句替代子查询,可以提高查询效率。例如,将以下子查询:
```
(select mainpd_id, sum(masks)layer from f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL group by mainpd_id) as c
```
改写为:
```
FROM f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL c GROUP BY mainpd_id
```
2.使用索引
对于频繁查询的字段,可以添加索引以提高查询效率。例如,可以为以下字段添加索引:
```
b.mainpd_id
a.lot_id
a.cust_id
lot_hs.customer_id
cc.cust_id_define
cc.mainpd_id
cc.prodspec_id
cc.custprod_id_45
```
3.使用 EXPLAIN 分析查询计划
使用 EXPLAIN 命令可以分析查询计划,了解查询语句的执行情况,从而优化查询语句以提高查询效率。
4.使用 UNION 替代多个 OR 条件
对于多个 OR 条件的查询语句,可以使用 UNION 来代替,可以提高查询效率。例如:
```
AND lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category in ('Ship','Unship')
```
可以改写为:
```
AND lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category = 'Ship'
UNION
AND lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category = 'Unship'
```
18-35 或空白值 通过SQL把这个值根据-分开,取右边的值,与50比大小,case when 返回1或0,不能使用SUBSTRING_INDEX函数
你可以使用`REVERSE`和`LOCATE`函数来实现这个需求。以下是一个示例的SQL查询语句:
```sql
SELECT CASE WHEN RIGHT(REVERSE(column_name), LOCATE('-', REVERSE(column_name)) - 1) > 50 THEN 1 ELSE 0 END AS result
FROM your_table;
```
请将`column_name`替换为包含你要拆分的字符串的列名,`your_table`替换为你的表名。这个查询会将字符串根据"-"分隔,取右边的值,并与50进行比较,返回1或0作为结果。
阅读全文