给大家安利一款数据库的工具 DBeaver,用过一次,就会爱上它,嘿嘿。 下载地址 => https://dbeaver.io/.
1.SQL执行顺序
执行顺序:
- from table
- where 条件限定
- group by … having … 分组过滤
- select 语句
- order by desc/asc
2.聚合函数
- max() 最大
- min() 最小
- avg() 平均值
- sum() 求和
- count() 数量
聚合函数的执行在group by之后,having之前
3.group by … having …
1.需求:数据库中每个数据都是两笔,版本迭代,记录上一版和当前版,判断依据就是version = ‘N’ or vesion = ‘Y’,找出只有一笔记录的数据(它对应的操作就是insert or delete)
2.sql语句
select data_group_id ,business_name ,count(business_name) as num FROM MMT_Data_G mdg
where configuration_id = 38 and data_group_type_cd = 'xxxx' GROUP BY business_name,data_group_id
HAVING count(business_name) = 1
4.报警
1.Column 'xxxx' is invalid in the select list because it is not contained in either an aggregate
=> 将select 中选择的栏位都要写入group by语句中
|