1. 程式人生 > >單世民第二次練習(練習資料初始化.sql)

單世民第二次練習(練習資料初始化.sql)

--工資排序  頭十個人
select top 10 empno,ename,sal
from emp
order by sal desc
--排名在第六到第十的  
with t as(select top 10 row_number() over (order by sal desc)xh,ename,sal
from emp
order by sal desc)
select *
from t
where xh>=6 and xh<=10


--尋找隨機數產生函式
select rand()
as '隨機數'
select cast( floor(rand()*100) as int)
select cast(ceiling(rand() * 100) as int)


--哪個部門提供了anlyst clerk manager三個職位
select distinct job
from emp
--包含這三個職位的部門
select distinct dname
from emp join dept
on emp.DEPTNO  = dept.DEPTNO 
where job in ('analyst','clerk','manager')


--每個職位都包含的部門
(select distinct dname
from emp join dept
on emp.DEPTNO  = dept.DEPTNO 
where job in ('analyst'))intersect(select distinct dname
from emp join dept
on emp.DEPTNO  = dept.DEPTNO 
where job in ('clerk'))intersect(select distinct dname
from emp join dept
on emp.DEPTNO  = dept.DEPTNO 
where job in ('manager'))


--找到每一個集合的包含關係  也就是每一個部門號包含的職位  a 並b 得a   a交b  得b   a-b 不為空集  b-a為空集
--提供工資在兩千到五千職位的部門(方法一)
--相當於對於dept表的每一行 一個deptno下  尋找與emp表同一個deptno的job   如果a-b為空集 說明後者包含前者  也就是說對於某一個特定部門 後者的job包含兩千到五千人員job的所有範圍 
--所以可以把部門號輸出
select dname
from dept
where not exists (select job
from emp
where sal<=5000 and sal>=2000
except
select job
from emp
where deptno = dept.deptno )


---輸出每個部門員工數 每個部門工資總和和平均工資  先顯示research部門,再將其他部門按字典序排序
select dname,count(empno) empcnt,sum(sal) sumsal,sum(comm) sumcomm
from dept left join emp on dept.deptno=emp.deptno
group by dname
order by case dname when 'RESEARCH' then 1
else 2 

end,dname;

--再把每個部門有哪些職位及對應多少人
--行列轉換
with a as
(select empno,deptno

,case job when 'ANALAYT' then 1 end ANALAYT --不滿足條件返回null
,case job when 'CLERK' then 1 end CLERK
,case job when 'MANAGER' then 1 end MANAGER
,case job when 'PRESIDENT' then 1 end PRESIDENT
,case job when 'SALESMAN' then 1 end SALESMAN
from emp)
select deptno,sum(ANALAYT) ANALATY --聚集函式忽略null
,sum(CLERK) CLERK
,sum(MANAGER) MANAGER
,sum(PRESIDENT) PRESIDENT
,sum(SALESMAN) SALESMAN
from a

group by deptno


--select into儲存一個新的表,並刪除
with a as
(select empno,deptno
,case job when 'ANALAYT' then 1 end ANALAYT --不滿足條件返回null
,case job when 'CLERK' then 1 end CLERK
,case job when 'MANAGER' then 1 end MANAGER
,case job when 'PRESIDENT' then 1 end PRESIDENT
,case job when 'SALESMAN' then 1 end SALESMAN
from emp)
select deptno,sum(ANALAYT) ANALATY --聚集函式忽略null
,sum(CLERK) CLERK
,sum(MANAGER) MANAGER
,sum(PRESIDENT) PRESIDENT
,sum(SALESMAN) SALESMAN
into newtb2
from a
group by deptno  --要是隻想要框架,不想要內容,再加一句having 1=2;
having 1=2


drop table newtb2


--檢視:簡化查詢的資料庫物件
create view vw_pivot as   --現查詢,實際上相當於查詢了一遍查詢語句,是不儲存資料的
select deptno,sum(ANALAYT) ANALATY --聚集函式忽略null
,sum(CLERK) CLERK
,sum(MANAGER) MANAGER
,sum(PRESIDENT) PRESIDENT
,sum(SALESMAN) SALESMAN
from(
select empno,deptno
,case job when 'ANALAYT' then 1 end ANALAYT --不滿足條件返回null
,case job when 'CLERK' then 1 end CLERK
,case job when 'MANAGER' then 1 end MANAGER
,case job when 'PRESIDENT' then 1 end PRESIDENT
,case job when 'SALESMAN' then 1 end SALESMAN
from emp)a
group by deptno;


select *
from vw_pivot


drop view vw_pivot


select empno,ename,sal
from emp


create view vw_1000sal as
select empno,ename,sal
from emp
where sal>1000
--with check option;  --原來滿足檢視條件的行,經過更改了不滿足查詢條件,就不允許這個更改


select empno,ename,sal
from vw_1000sal


begin transaction
update vw_1000sal set sal=sal+1000 --update vw_2500sal set sal=sal+1000(只有滿足檢視條件的才會更改)
select * from vw_1000sal


rollback;

--begin transaction  以及檢視相關東西還沒來得及整理  後續整理