在DataGrid中显示的数据如下 :编号 姓名 打卡日期 打卡时间
A01 A 03/01/06 12:18
A01 A 03/01/06 13:22
A01 A 03/01/06 17:29
以上表有三行,现在想只汇总一条记录,为:编号 姓名 打卡日期 打卡时间1 打卡时间2 打卡时间3
A01 A 03/01/06 12:18 13:22 17:29请问代码该怎样写呢?谢谢.
A01 A 03/01/06 12:18
A01 A 03/01/06 13:22
A01 A 03/01/06 17:29
以上表有三行,现在想只汇总一条记录,为:编号 姓名 打卡日期 打卡时间1 打卡时间2 打卡时间3
A01 A 03/01/06 12:18 13:22 17:29请问代码该怎样写呢?谢谢.
insert #t select 'A01', 'A', '03/01/06', '12:18'
insert #t select 'A01', 'A', '03/01/06', '13:22'
insert #t select 'A01', 'A', '03/01/06', '17:29'
insert #t select 'B01', 'B', '03/01/06', '12:18'
insert #t select 'B01', 'B', '03/01/06', '13:22'
insert #t select 'B01', 'B', '03/01/06', '17:29'
select b.编号,b.姓名,b.打卡日期,
打卡时间1=max(case when b.id=1 then 打卡时间 end),
打卡时间2=max(case when b.id=2 then 打卡时间 end),
打卡时间3=max(case when b.id=3 then 打卡时间 end)
from
(select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间>=a.打卡时间),* from #t a) b
group by b.编号,b.姓名,b.打卡日期drop table #t
/*结果编号 姓名 打卡日期 打卡时间1 打卡时间2 打卡时间3
---------- ---------- --------------------------------- ---------- ---------- ----------
A01 A 2003-01-06 00:00:00.000 17:29 13:22 12:18
B01 B 2003-01-06 00:00:00.000 17:29 13:22 12:18(所影响的行数为 2 行)*/
你给的代码可以做到,可现在如果一天A 打了5资,B才打了3次,用你的代码就会出现问题,请问你可以帮忙解决吗?也就是说一个人一天打卡次数是不固定的.谢谢.
insert #t select 'A01', 'A', '03/01/06', '12:18'
insert #t select 'A01', 'A', '03/01/06', '13:22'
insert #t select 'A01', 'A', '03/01/06', '17:29'
insert #t select 'A01', 'A', '03/01/06', '18:18'
insert #t select 'A01', 'A', '03/01/06', '19:22'
insert #t select 'A01', 'A', '03/01/06', '20:29'
insert #t select 'B01', 'B', '03/01/06', '12:18'
insert #t select 'B01', 'B', '03/01/06', '13:22'
insert #t select 'B01', 'B', '03/01/06', '17:29' declare @sql varchar(8000)
set @sql='select b.编号,b.姓名,b.打卡日期,'
select @sql=@sql+'打卡时间'+cast(b.id as varchar(10))+'=max(case when b.id='+cast(b.id as varchar(10))+' then 打卡时间 end),'
from
(select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间>=a.打卡时间),* from #t a) b
group by b.id
set @sql=left(@sql,len(@sql)-1)
+' from (select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间>=a.打卡时间),* from #t a) b
group by b.编号,b.姓名,b.打卡日期'
exec(@sql)
drop table #t/*结果编号 姓名 打卡日期 打卡时间1 打卡时间2 打卡时间3 打卡时间4 打卡时间5 打卡时间6
---------- ---------- ------------------------------ ----- ----- ----- ----- ----- -----
A01 A 2003-01-06 00:00:00.000 20:29 19:22 18:18 17:29 13:22 12:18
B01 B 2003-01-06 00:00:00.000 17:29 13:22 12:18 NULL NULL NULL警告: 聚合或其它 SET 操作消除了空值。
*/
依打卡次数多少确定列数
create table #t(编号 varchar(10),姓名 varchar(10),打卡日期 datetime,打卡时间 varchar(5))
insert #t select 'A01', 'A', '03/01/06', '12:18'
insert #t select 'A01', 'A', '03/01/06', '13:22'
insert #t select 'A01', 'A', '03/01/06', '17:29'
insert #t select 'A01', 'A', '03/01/06', '18:18'
insert #t select 'A01', 'A', '03/01/06', '19:22'
insert #t select 'A01', 'A', '03/01/06', '20:29'
insert #t select 'B01', 'B', '03/01/06', '12:18'
insert #t select 'B01', 'B', '03/01/06', '13:22'
insert #t select 'B01', 'B', '03/01/06', '17:29' declare @sql varchar(8000)
set @sql='select b.编号,b.姓名,b.打卡日期,'
select @sql=@sql+'打卡时间'+cast(b.id as varchar(10))+'=max(case when b.id='+cast(b.id as varchar(10))+' then 打卡时间 end),'
from
(select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间>=a.打卡时间),* from #t a) b
group by b.id
set @sql=left(@sql,len(@sql)-1)
+' from (select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间>=a.打卡时间),* from #t a) b
group by b.编号,b.姓名,b.打卡日期'
exec(@sql)
drop table #t
create table #t(编号 varchar(10),姓名 varchar(10),打卡日期 datetime,打卡时间 varchar(5))
insert #t select 'A01', 'A', '03/01/06', '12:18'
insert #t select 'A01', 'A', '03/01/06', '13:22'
insert #t select 'A01', 'A', '03/01/06', '17:29'
insert #t select 'A01', 'A', '03/01/06', '18:18'
insert #t select 'A01', 'A', '03/01/06', '19:22'
insert #t select 'A01', 'A', '03/01/06', '20:29'
insert #t select 'B01', 'B', '03/01/06', '12:18'
insert #t select 'B01', 'B', '03/01/06', '13:22'
insert #t select 'B01', 'B', '03/01/06', '17:29' declare @sql varchar(8000)
set @sql='select b.编号,b.姓名,b.打卡日期,'
select @sql=@sql+'打卡时间'+cast(b.id as varchar(10))+'=max(case when b.id='+cast(b.id as varchar(10))+' then 打卡时间 end),'
from
(select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间<=a.打卡时间),* from #t a) b
group by b.id
set @sql=left(@sql,len(@sql)-1)
+' from (select id=(select count(*) from #t where 编号=a.编号 and 打卡日期=a.打卡日期 and 打卡时间<=a.打卡时间),* from #t a) b
group by b.编号,b.姓名,b.打卡日期'
exec(@sql)drop table #t
问一下,这条语句是什么意思...?汉一下自己!
3 A01 A 2003-01-06 00:00:00.000 12:18
2 A01 A 2003-01-06 00:00:00.000 13:22
1 A01 A 2003-01-06 00:00:00.000 17:29
3 B01 B 2003-01-06 00:00:00.000 12:18
2 B01 B 2003-01-06 00:00:00.000 13:22
1 B01 B 2003-01-06 00:00:00.000 17:29
不明白为什么id会出现123这写值~~我以为会出现id=6!!!忘回答....