SQL编码规范

SQL编码规范

为方便代码的快速迭代和代码的review

- 禁止使用```select *```

- 表的别名尽量有意义

- 中间对齐

```sql

select t1.co1,t2.co1

  from t1

  left join t2

    on t1.id=t2.id

   and t2.partition_dt=''

 where t1.partition_dt=''

```

- 代码宽度不超过一屏

```sql

select t1.col1,t1.col2,t1.col3,t1.col4,t1.col5,t1.col6,t1.col7,t1.col8,t1.col9,t1.col10,t1.col11,t1.col12,t1.col13,t1.col14

  from t1

```

- 列数量多的时候,只在单独行展示实际加工的列

```sql

select t1.col1,t1.col2,t1.col3,t1.col4,t1.col5,t1.col6,t1.col7,t1.col8,t1.col9,t1.col10,t1.col11,t1.col12,t1.col13,t1.col14,

if(t1.col>0,1,0),

case when t1.col<=0 and t1.col>-3 then t2.col end  

  from t1

```

- 同一表多次使用的情况用with as()代码块替代

```sql

with t1 as (

     select t1.cd,t1.td,t1.col

       from t1

      where partition_dt=''

)

select t2.col,t1.col

  from t2

  left join t1

    on t2.id=t1.id

   and t1.cd='1'

  left join t1

    on t2.id=t1.id

   and t1.td='2'

 where t2.partition_dt=''

```

- 使用空格代替```tab```健(tab健在不同的编辑器空的不一样多)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容