yuangong 表
yid yname ygongzi buid
bumen表
buid buname
1查询 每个部门总工资大于1100 的所有员工信息
select * from yuangong where yuangong.buid=
(select b.buid from yuangong as a inner join bumen as b on a.buid=b.buid group by b.buid having sum(a.gongzi)>1100)
注意:select 字段必须是group by 后的字段 ,条子是聚合函数时 放在having后
2查询出工资是500 重复次数
select gongzi,count(gongzi) from yuangong group by gongzi having count(gongzi)>1 and gongzi=500
月份统计
电量表 dian
Did Dnum time
1 8 2011/5/1
统计2011年每个月的电量总和
select month(time) as months,sum(dnum) as sums from dian where year(time)=2011 group by month(time)
结果
months sums
5 216
6 5
竖向年份 横向月份 统计
select a.year as 年份,sum(case when a.month=4 then dnum end) as [4],[5]=sum(case when a.month=5 then a.dnum end) from
(select year=year(time),month=month(time), dnum from dian)as a group by year
注意当命名字段名时数字必须加中括号[]
select convert(char(10),GetDate(),120) as Date
* 第3个参数就是用来设置日期类型数据的显示样式的,下面介绍几种样式的参数:
100 mm dd yyyy
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
106 dd mm yyyy
108 hh:mi:ss(时间)
111 yyyy/mm/dd
112 yyyymmdd
120 yyyy-mm-dd