我要做网站推广/搜索大全搜索引擎
目录
- 一、题目
- 二、解决
- 1、row_number() + max(case when then end)
- 2、left join
- 3、表格格式化问题学习
- 1. 表格转换问题
- 2. 设计序列构建新表格问题
- 三、参考
一、题目
二、解决
1、row_number() + max(case when then end)
思路:
max(): 确保在分组后的多行不遗漏,如果不加只会选择第一行,剩下的会被忽略.
代码:
select max(case when continent = 'America' then name end) as America,max(case when continent = 'Asia' then name end) as Asia,max(case when continent = 'Europe' then name end) as Europe
from
(select *, row_number() over (partition by continent order by name) rk from student
) t
group by rk
2、left join
思路: 略.
代码:
select America, Asia, Europe from (selectname as America,row_number() over(order by name) as rnfrom student where continent = 'America'
) t1 left join (selectname as Europe,row_number() over(order by name) as rnfrom student where continent = 'Europe'
) t2 on t1.rn = t2.rn left join (selectname as Asia,row_number() over(order by name) as rnfrom student where continent = 'Asia'
) t3 on t1.rn = t3.rn;
3、表格格式化问题学习
1. 表格转换问题
618.学生地理信息报告(困难);
1179.重新格式化部门表(简单);
1435.制作会话柱状图(简单);
1777.每家商店的产品价格(简单);
1795.Rearrange Product Table(简单)
1.1 行转列问题
通用解题技巧1:group by+sum/max/min(case when)
tips:原表格有基准id如product_id,department_id等。通用解题技巧2:row_number()+group by+max(if)
tips:原表格没有基准id需要自己构造。
1.2 列转行问题
通用解题技巧:union all
2. 设计序列构建新表格问题
1127.用户购买平台(困难);
1336.每次访问的交易次数(困难);
1384.按年度列出销售总额(困难);
1613.找到遗失的ID(中等)
1635.Hopper公司查询I(困难);
1767.Find the subtasks that did not execute(困难)
2.1 设计序列构建新表格问题1:数列构建
通用解题技巧:with语句+left join
tips:result表中出现无中生有的列,且列中的数据为一个逐渐递增的数列,考虑使用with方法
2.2 设计序列构建新表格问题2:字符串构建
通用解题技巧:union all+left join
tips:result表中出现无中生有的列,且列中的数据为未曾出现过的字符串,考虑使用union all方法
三、参考
1、总结各类表格格式化问题
2、原题+进阶解法整合
3、三种方法分析(解题+进阶)——学生地理信息报告