日期:2014-05-19  浏览次数:20481 次

请求高手指教一二
---创建测试数据
declare   @t   table([name]   varchar(10),birth   datetime)
insert   @t   select   '张先生 ', '1985-10-4 '  
union   all   select   '林先生 ', '1985-11-5 '
union   all   select   '卢先生 ', '1985-05-7 '  
union   all   select   '刘先生 ', '1985-01-8 '
union   all   select   '李先生 ', '1985-01-9 '
union   all   select   'X先生 ', '1985-01-9 '

---查看测试数据
--select   *   from   @t

---查看结果
select  
              no,
              max(case   when   month(birth)   =   '01 '   then   [name]   else   ' '   end)   as   [01],
              max(case   when   month(birth)   =   '02 '   then   [name]   else   ' '   end)   as   [02],
              max(case   when   month(birth)   =   '03 '   then   [name]   else   ' '   end)   as   [03],
              max(case   when   month(birth)   =   '04 '   then   [name]   else   ' '   end)   as   [04],
              max(case   when   month(birth)   =   '05 '   then   [name]   else   ' '   end)   as   [05],
              max(case   when   month(birth)   =   '06 '   then   [name]   else   ' '   end)   as   [06],
              max(case   when   month(birth)   =   '07 '   then   [name]   else   ' '   end)   as   [07],
              max(case   when   month(birth)   =   '08 '   then   [name]   else   ' '   end)   as   [08],
              max(case   when   month(birth)   =   '09 '   then   [name]   else   ' '   end)   as   [09],
              max(case   when   month(birth)   =   '10 '   then   [name]   else   ' '   end)   as   [10],
              max(case   when   month(birth)   =   '11 '   then   [name]   else   ' '   end)   as   [11],
              max(case   when   month(birth)   =   '12 '   then   [name]   else   ' '   end)   as   [12]
from   (
select   *,(select   count(*)   from   @t   where   month(birth)=month(a.birth)   and   (birth <a.birth
or   birth=a.birth   and   name <=a.name))   as   No   from   @t   a
)   as   t
group   by   no

--结果
no