group by 隨機數 查詢保存到臨時表

select * from dbo.Employee

insert into dbo.Employee  values 
('name9',10,'男',2,2, cast(ceiling(rand() *4000) as int)+1000),
('name10',10,'女',2,2,cast(ceiling(rand() *4000) as int)+1000),
('name11',10,'男',2,2,cast(ceiling(rand() *4000) as int)+1000),
('name12',10,'女',2,2,cast(ceiling(rand() *4000) as int)+1000),
('name13',10,'男',2,2,cast(ceiling(rand() *4000) as int)+1000),
('name14',10,'男',2,2,cast(ceiling(rand() *4000) as int)+1000),
('name15',10,'女',2,2,cast(ceiling(rand() *4000) as int)+1000)

select AVG(age) from dbo.Employee where sex='男'


select * from Employee m,Education d
	where m.Education=d.id
	and d.EduName='本科'
	
	select sex,MAX(e.Salary), MIN(e.Salary) from Employee e
		group by e.Sex
	
	--	男女各多少人
	select COUNT(1) ,sex 
		from Employee
		group by sex
	
	--	每個崗位有多少人
	select COUNT(1), j.JobName
		from Employee e,Job  j
		where e.Job=j.id
		group by j.JobName
	--	每個學歷有多少人
	select COUNT(1),d.EduName
		from	Employee e, Education d
		where e.Education=d.id
		group  by d.EduName
	--	每個崗位每個月發多少錢
	select SUM(salary),j.JobName
		from Employee e,Job j
		where e.Job=j.id
		group by j.JobName
	
	--	那個崗位發的錢最多
	select top 1 SUM(salary),j.JobName
		from Employee e,Job j
		where e.Job=j.id
		group by j.JobName
		order by SUM(salary) desc
	
	
	
		
		-- 把查詢到數據插入到一張表裏面
		-- 如果表存在
			insert into [新表名]
				select [列名] from [原表名]
		-- 如果表不存在
			select [列名] into [新表名]
				from[原表名]
						

發佈了43 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章