核心思想:嵌套
子查询常规用法
SELECT * FROM (
SELECT
SUBSTR(Product,9,2) AS asd,
SUBSTR(Product,LOCATE(' ',Product)+1,2) AS asf,
Product
Product FROM new_schema20200309.new_table) AS new_table_1
这里将里面一层SELECT作为一个临时生成的table,然后再对他进行SELECT。
注意:子查询一定要设置表别名,不然会报错
这里我们就可以使用子查询直接引用 asd 和 asf
CASE-WHEN-ELSE-END (类似于Python中的if else)
可以进行缩进(便于观察),也可以不缩进
SELECT
CASE
WHEN (bottom + top)/2 <=10 THEN '0~10'
WHEN (bottom + top)/2 <=20 THEN '10~20'
WHEN (bottom + top)/2 <=30 THEN '20~30'
ELSE '30+'
END,
salary FROM(
SELECT
LEFT(salary,LOCATE("k",salary)-1 as bottom,
SUBSTR(salary,LOCATE("-",salary)+1,LENGTH(salary)-LOCATE("-",salary)-1) as top,
salary FROM data.dataanalyst as t
结果
case | salary |
---|---|
10~20 | 8k-15k |
10~20 | 10k-18k |
0~10 | 5k-11k |
30+ | 20k-50k |
… | … |
20~30 | 15k-30k |