您的位置:程序门 -> ms-sql server -> 基础类



超小问题,马上解决,马上给分,谢谢大家


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


超小问题,马上解决,马上给分,谢谢大家[已结贴,结贴人:tel100]
发表于:2007-06-28 09:51:46 楼主
现有考勤表a    
id(id)         用户名(name)         考勤类型(int     sort)         考勤时间(time)     部门(     int     dept)    
      1                         张三                                                         1(迟到)                                         2005-05-05                             3(市场部)    
      2                         李四                                                         1(迟到)                                         2005-06-05                             2(开发部)    
      3                         张三                                                         2(旷工)                                         2005-05-05                             3(市场部)    
      4                     李四                                                             2(旷工)                                         2005-06-05                             3(市场部)    
考勤分类表b    
bid             考勤类型    
  1                         迟到    
  2                         旷工    
  3                         早退    
  4                         病假    
  ...                 ...    
如何按时间或部门分类汇总:    
 
用户名                     迟到                 旷工             早退                     病假        
      张三                         1                             1                         0                                     0                             //数字表示次数    
      李四                         1                             2                         0                                     0
发表于:2007-06-28 10:05:061楼 得分:5

create   table   考勤表a     (id   int,name   nvarchar(30),   sort   int,   考勤时间   datetime     ,   dept   int     )

insert   考勤表a
select         1,n '张三 '                                   ,                       1           ,                 2005-05-05               ,               3
union   select         2,               n '李四 '               ,                                           1,                                 2005-06-05     ,                         2
union   select         3,               n '张三 '               ,                                           2,                   2005-05-05                   ,           3
union   select         4,n '李四 '                 ,                                             2,                                 2005-06-05           ,                   3

create   table   考勤分类表b     (bid   int,   考勤类型     nvarchar(10))
insert   考勤分类表b
select     1               ,         n '迟到 '    
union   select     2             ,           n '旷工 '    
union   select     3           ,               n '早退 '    
union   select     4               ,         n '病假 '    


select   用户名=a.name,
迟到=sum(case   when   sort=1   then   1   else   0   end)   ,  
旷工=sum(case   when   sort=2   then   1   else   0   end)   ,              
早退=sum(case   when   sort=3   then   1   else   0   end)   ,                    
病假=sum(case   when   sort=4   then   1   else   0   end)    
from   考勤表a   a   inner   join   考勤分类表b   b   on   a.sort=b.bid
group   by   a.name


drop   table   考勤表a,考勤分类表b
发表于:2007-06-28 10:06:052楼 得分:0
用動態的sql
发表于:2007-06-28 10:08:153楼 得分:0
--如果考勤类型是固定的
select
a.name,
sum(case   b.考勤类型   when   '迟到 '   then   1   else   0   end)   as   迟到,
sum(case   b.考勤类型   when   '旷工 '   then   1   else   0   end)   as   旷工,
sum(case   b.考勤类型   when   '早退 '   then   1   else   0   end)   as   早退,
sum(case   b.考勤类型   when   '病假 '   then   1   else   0   end)   as   病假
from
a
inner   join
b
on   a.sort   =   b.bid
group   by
a.name
发表于:2007-06-28 10:08:294楼 得分:5
if   object_id( 'tbtest ')   is   not   null
drop   table   tbtest
if   object_id( 'tbtype ')   is   not   null
drop   table   tbtype
go
create   table   tbtest(id   int,name   varchar(10),sort   int,time   varchar(10),dept   int)
insert   tbtest
select   1,   '张三 ',   1,   '2005-05-05 ',   3   union   all
select   2,   '李四 ',   1,   '2005-06-05 ',   2   union   all
select   3,   '张三 ',   2,   '2005-05-05 ',   3   union   all
select   4,   '李四 ',   2,   '2005-06-05 ',   3
create   table   tbtype(bid   int,考勤类型     varchar(10))
insert   tbtype
select   1,                         '迟到 '   union   all    
select   2,                         '旷工 '   union   all
select   3,                         '早退 '   union   all
select   4,                         '病假 '    
go
发表于:2007-06-28 10:08:375楼 得分:0
select   *   from   tbtest
declare   @sql   varchar(8000)
set   @sql   =   'select   a.name   as   用户名 '
select   @sql   =   @sql   +   ', '   +   考勤类型   +   '=   isnull(sum(case   when   sort   =   '   +   rtrim(bid)   +   '   then   1   end),0) '
from   tbtype  
set   @sql   =   @sql   +   '   from   tbtest   as   a   left   join   tbtype   as   b   on   a.sort   =   b.bid   group   by   a.name '
print   @sql
EXEC(@sql)

drop   table   tbtest,tbtype
发表于:2007-06-28 10:09:506楼 得分:0
/*结果
用户名                     迟到                 旷工             早退                     病假  
------------------------------------------------------------      
张三                         1                       1                     0                           0    
李四                         1                       1                     0                           0
*/


----回个帖子要费牛劲,老是发送不成功,得拆得乱七八糟才行.@_@
发表于:2007-06-28 10:10:227楼 得分:0
--如果考勤类型不是固定的
declare   @s   nvarchar(4000)
select   @s   =   '   select   a.name '
select   @s   =   @s   +   ',sum(case   b.考勤类型   when   ' ' '   +   考勤类型   +   ' ' '   then   1   else   0   end)   as   '   +   考勤类型
from   b   group   by   考勤类型
select   @s   =   @s   +   '   from   a   inner   join   b   on   a.sort   =   b.bid   group   by   a.name '
EXEC(@s)
发表于:2007-06-28 10:10:398楼 得分:18
参考

create   table   tab   (姓名   varchar(20),项目     varchar(1),   金额   int)
insert   into   tab   values( 'a ', '1 ',66)
insert   into   tab   values( 'a ', '2 ',44)
insert   into   tab   values( 'b ', '1 ',88)
insert   into   tab   values( 'b ', '2 ',99)

select   *   from   tab

declare   @sql   varchar(max)
set   @sql= 'select   姓名, '
select   @sql=@sql+ 'sum(case   when   项目= ' ' '+   项目+ ' ' '   then   金额   else   0   end   )   as   项目 '+   项目   + ', '   from   tab   group   by   项目
set   @sql=left(@sql,len(@sql)-1)+ '   from   tab   group   by   姓名 '
EXEC(@sql)

drop   table   tab
发表于:2007-06-28 10:12:169楼 得分:2

create   table   a

id   int   identity(1,1),
name   varchar(5),
sort   int   ,
time   datetime,
dept   int
)    
go  
insert   into   a  
select   '张三 ',1, '2005-05-05 ',3   union   all
select   '李四 ',2, '2005-06-05 ',2   union   all
select   '张三 ',2, '2005-05-05 ',3   union   all
select   '李四 ',2, '2005-06-05 ',3
go
create   table   b  

bid   int   identity(1,1),
tp   varchar(5)
)
insert   into   b
select   '迟到 '   union   all
select   '旷工 '   union   all
select   '早退 '   union   all
select   '病假 '  
go
declare   @sql   varchar(500)
set   @sql= 'select   name '
select   @sql=@sql+ ',sum(case   tp   when   ' ' '+tp+   ' ' 'then   1   else   0   end)   as   '   +tp   from  
(select   distinct   tp   from   b)   tb
set     @sql   =@sql+ '   from   (select   a.*,b.tp   from   a   left   outer   join   b   on   a.sort=b.bid)   tb   group   by   name '--此处按部门统计改为group   by   dept,name,按时间将where   条件更改一下   where   time   between   ...and   ....  
EXEC   (@sql)
name     病假                     迟到                     旷工                     早退
-----   -----------   -----------   -----------   -----------
李四         0                       0                       2                       0
张三         0                       1                       1                       0

(2   行受影响)
发表于:2007-06-28 10:15:5210楼 得分:0
谢谢
考勤类型是不固定的.
发表于:2007-06-28 10:27:0111楼 得分:0
create   table   a(id   int,name   nvarchar(10),sort   int,time   varchar(10),dept   int)
insert   a   select   1,   n '张三 ',   1,   '2005-05-05 ',   3  
union   all   select   2,   n '李四 ',   1,   '2005-06-05 ',   2  
union   all   select   3,   n '张三 ',   2,   '2005-05-05 ',   3  
union   all   select   4,   n '李四 ',   2,   '2005-06-05 ',   3
create   table   b(bid   int,考勤类型     nvarchar(10))
insert   b   select   1,                         n '迟到 '  
union   all     select   2,                         n '旷工 '  
union   all     select   3,                         n '早退 '  
union   all     select   4,                         n '病假 '    
go
--如果考勤类型是固定的
select
a.name,
sum(case   b.考勤类型   when   n '迟到 '   then   1   else   0   end)   as   迟到,
sum(case   b.考勤类型   when   n '旷工 '   then   1   else   0   end)   as   旷工,
sum(case   b.考勤类型   when   n '早退 '   then   1   else   0   end)   as   早退,
sum(case   b.考勤类型   when   n '病假 '   then   1   else   0   end)   as   病假
from
a
inner   join
b
on   a.sort   =   b.bid
group   by
a.name

--如果考勤类型不是固定的
declare   @s   nvarchar(4000)
select   @s   =   '   select   a.name '
select   @s   =   @s   +   n ',sum(case   b.考勤类型   when   n ' ' '   +   考勤类型   +   ' ' '   then   1   else   0   end)   as   '   +   考勤类型
from   b   order   by   bid
select   @s   =   @s   +   '   from   a   inner   join   b   on   a.sort   =   b.bid   group   by   a.name '
EXEC(@s)
go
drop   table   a,   b
--result
/*
name 迟到 旷工 早退 病假
李四 1 1 0 0
张三 1 1 0 0
*/
发表于:2007-06-28 10:47:3712楼 得分:0
谢谢paoluo(一天到晚游泳的鱼)及所有的朋友
发表于:2007-06-28 10:50:0513楼 得分:0
這。。。

暈倒。
发表于:2007-06-28 11:59:3614楼 得分:0
:)涮鱼...


快速检索

热门点击