1.算列里面两者差值最小
select Min(ABS(x.x-y.x)) from test1 x join test1 y where x.x != y.x
2.行转列(case语句的用法)
Case具有两种格式。简单Case函数和Case搜索函数。
–简单Case函数
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
eg:
CASE sex WHEN ‘1’ THEN ‘男’ WHEN ‘2’ THEN ‘女’ ELSE ‘其他’ END
搜索Case函数
CASE WHEN [expr] THEN [result1]…ELSE [default] END
eg:
CASE WHEN sex = ‘1’ THEN ‘男’ WHEN sex = ‘2’ THEN ‘女’ ELSE ‘其他’ END
select 学号, sum(case 科目 when "数学" then 分数 end) as "数学", sum(case 科目 when "语文" then 分数 end) as "语文", sum(case 科目 when "英语" then 分数 end) as "英语" from test2 group by 学号
3.列转行
select 学号, 数学 as 分数, "数学" as 科目 from test3 union
select 学号, 语文 as 分数, "语文" as 科目 from test3 order by 学号