1. 程式人生 > >經典SQL語句彙總

經典SQL語句彙總

      DROP TABLE #table

3、企業部門員工人數逐級彙總

    select deptName,deptcode,
             (select  sum(PeopleSum) as PeopleSum from
                (select b.dept_id,b.deptcode,a.PeopleSum
                   from
                         (select dept_id,count(*) as PeopleSum 
                               from hr_empl
                               group by dept_id)a 
                         right join dept b on a.dept_id = b.dept_id
               order by b.deptcode) c  where c.deptcode like a.deptcode||'%' ) as  PeopleSum
    from dept a
    order by a.deptcode

4、用取模的方式巧妙分組統計
   select YMDHM,SUM(L0) as SumL0 from Tb_Qry_Sal_FactPower where ymdhm='2008-01-01' group by YMDHM,mm/15