一个参数 VALUE 有三个值 : 请假 \ 缺勤 \早退
如果 我想 按  姓名   请假总数  缺勤总数 早退总数
这样用一条SQL语句来得出一段时间的所以人员的数据,这句话应该怎么写  请指教

解决方案 »

  1.   

    假设保存"请假\缺勤\早退"的列名为value:
    select 姓名,'请假总数'=count(case value when '请假' then 姓名 else null end),
    '缺勤总数'=count(case value when '缺勤' then 姓名 else null end),
    '早退总数'=count(case value when '早退' then 姓名 else null end)
    from tablename  
    where ... group by 姓名      /*将"..."替换成条件*/
      

  2.   

    --参数?如果是字段的话可以用类似下面的语句select
    A.EmployeeName as 姓名,
    isnull(count(case ItemName when '请假' then 1 else 0 end),0) as 请假总数,
    isnull(count(case ItemName when '缺勤' then 1 else 0 end),0) as 缺勤总数,
    isnull(count(case ItemName when '早退' then 1 else 0 end),0) as 早退总数
    from
    Emplyee_Master A
    left outer join
    Work_Attendenct B
    on
    A.EmployeeID=B.EmployeeID
    --BTW 如果LZ很急的话,请尽量把自己的问题表述清楚,当然最好用词也准确点,
    --至于不要有错别字则是最基础的要求了,要知道欲速则不达,他人更好地理解你的问题后
    --才能及时给你准确的回复
      

  3.   

    --又忘写group了
    select
    A.EmployeeName as 姓名,
    isnull(count(case ItemName when '请假' then 1 else 0 end),0) as 请假总数,
    isnull(count(case ItemName when '缺勤' then 1 else 0 end),0) as 缺勤总数,
    isnull(count(case ItemName when '早退' then 1 else 0 end),0) as 早退总数
    from
    Emplyee_Master A
    left outer join
    Work_Attendenct B
    on
    A.EmployeeID=B.EmployeeID
    Group By 
    A.EmployeeName