首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请求高手指教一二,该怎么处理

2012-01-07 
请求高手指教一二---创建测试数据declare@ttable([name]varchar(10),birthdatetime)insert@tselect张先生

请求高手指教一二
---创建测试数据
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                     01                   02                   03                   04                   05                   06                   07                   08                   09                   10                   11                   12                  


-----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------   ----------  
1                       刘先生                                                                               卢先生                                                                                                   张先生                 林先生                
2                       X先生                                                                                                                                                                                                                                            
3                       李先生                                                                                                                                                                                                                                            

(所影响的行数为   3   行)
max(case   when   month(birth)   =   '01 '   then   [name]   else   ' '   end)   as   [01],   这句写max   只是要为了group   by   分组必须用一个聚合函数,还是有其他意义,有点不理解在这为什么要用max


[解决办法]
我明白你的意思,
這里不用MAX,用MIN結果也是一樣的,你可以試試,因為每個人的生日是唯一的。

[解决办法]
楼主:这里加MAX,主要你为了给NO 分组,
如果不加MAX的话,那么 Group By No 分组时会出错...因为它没有聚合函数

按NO分组的目的就是将相同的NO,合并成一行,如下面的例子
---创建测试
Declare @t Table(No int)
Insert @t Select 1
Union All Select 1
Union All Select 1


Union All Select 1
Union All Select 1
Union All Select 2
Union All Select 2
Union All Select 3
Select * From @t
Select * From @t Group By No
/*
(所影响的行数为 8 行)

No
-----------
1
1
1
1
1
2
2
3

(所影响的行数为 8 行)

No
-----------
1
2
3

(所影响的行数为 3 行)
*/

热点排行