SQL语句基础模板

新增字段:
alter table [表名] add [字段名] nvarchar (50) null
alter table [表名]
alter column [字段名] varchar(100) not null

–删除字段:
alter table [表名] drop column [字段名]

–修改字段:
alter table [表名] alter column [字段名] nvarchar (50) null

–重命名表:(access 重命名表,请参考文章:在access数据库中重命名表)
sp_rename ‘表名’, ‘新表名’, ‘object’

、 compute和compute by汇总查询

对年龄大于的进行汇总
select age from student
where age > 20 order by age compute sum(age) by age;

对年龄大于的按照性别进行分组汇总年龄信息
select id, sex, age from student
where age > 20 order by sex, age compute sum(age) by sex;

按照年龄分组汇总
select age from student
where age > 20 order by age, id compute sum(age);

按照年龄分组,年龄汇总,id找最大值
select id, age from student
where age > 20 order by age compute sum(age), max(id);
compute进行汇总前面是查询的结果,后面一条结果集就是汇总的信息。
compute子句中可以添加多个汇总表达式,可以添加的信息如下:

 a、 可选by关键字。它是每一列计算指定的行聚合

 b、 行聚合函数名称。包括sum、avg、min、max、count等

 c、 要对其执行聚合函数的列

 compute by适合做先分组后汇总的业务。compute by后面的列一定要是order by中出现的列。

更多推荐

SQL语句基础模板值得拥有