sql 常见用法

1. where and having

须要的条件 >=2 + 须要使用合计条件;sql

  • 分析哪些条件,要用合计函数运算;
  • where 中设立不须要合计;
  • 获得合计函数;
  • 设立group by 和 having 须要的合计函数条件;

2. Join 和 Aggregate Function

合计条件 + table数量 >= 2windows

  • join 的条件(join的类型和条件);
  • 合计函数条件(group by / Aggregate Func)

Join + group by + Aggregate Func函数

3. 重复值

windows function / distinct / join / group by / having clause ;code

  • (找重仍是去重);
  • distinct, group by 和 windows Func 去重;
  • 找重 : having 和 windows Func;
  • 简单粗暴 : 用window function (partial by 和 order by 排序, 而后找重或者去重)。
select * from table group by name having (count *) > 2

sql : qualify
where ---> Join ---> **qualify** ---> select

4. 找nth

windows function , max排序

  • 分析需求(求第几大,仍是第几小);
  • Windows func 和 max (partial by , order by) 排序;
  • 第几大,仍是第几小。
相关文章
相关标签/搜索