วันอาทิตย์ที่ 22 กุมภาพันธ์ พ.ศ. 2558

คำสั่ง Select ซ้อน SELECT คำสั่งในการหาข้อมูลการลาแยกประเภท



SELECT a.empno as empno,a.firstname as fistname ,a.lastname as lastname ,d.depName as dep,po.posname as posname,
(select sum(amount) FROM WORK WHERE a.empno=enpid and typela = 1) t1,
(select sum(amount) FROM WORK WHERE a.empno=enpid and typela = 2) t2 ,
(select sum(amount) from WORK where a.empno=enpid and typela = 3) t3 ,
(select sum(amount) from WORK where a.empno=enpid and typela = 4) t4 ,
(select sum(amount) from WORK where a.empno=enpid and typela = 5) t5 ,
(select sum(amount) from WORK where a.empno=enpid and typela = 6) t6 ,
(select sum(amount) from WORK where a.empno=enpid and typela = 7) t7 ,
(select count(typela) from WORK where a.empno=enpid and typela=1) l1 ,
(select count(typela) from WORK where a.empno=enpid and typela=2) l2 ,
(select count(typela) from WORK where a.empno=enpid and typela=3) l3 ,
(select count(typela) from WORK where a.empno=enpid and typela=4) l4 ,
(select count(typela) from WORK where a.empno=enpid and typela=5) l5 ,
(select count(typela) from WORK where a.empno=enpid and typela=6) l6 ,
(select count(typela) from WORK where a.empno=enpid and typela=7) l7 ,
(select sum(total) from timela where a.empno=empno ) time1 ,
(select count(*) from timela where a.empno=empno ) time2
from emppersonal a
left outer join department d on d.depid=a.depId
left outer join posid po on po.posId=a.posid  
LEFT OUTER JOIN work w on w.enpid=a.empno
GROUP BY a.empno