数据分析后插入新表,怎么实现?
表
id action number date
0001 Checkin X.521 2007-9-1
0001 Checkin F-2007 2007-9-1
0001 Checkout F-2007 2007-9-1
1045 Checkout X.Lt 2007-9-1
1045 Checkin G-/p 2007-9-1
1045 Checkout G-/p 2007-9-2
我现在需要根据id和date来统计该id当天checkin和checkout的情况,并记录对应的number 生成新表
id checkin checkin_number checkout cheout_number date
0001 2 X.521_F-2007 1 F-2007 2007-9-1
1045 1 G-/p 1 X.Lt 2007-9-1
1045 0 1 G-/p 2007-9-2
.....
这样的函数怎么写啊?
------解决方案--------------------create table a (id varchar(100),action varchar(20), number varchar(100),date smalldatetime)
insert a select '0001 ', 'Checkin ', 'X.521 ', '2007-9-1 '
union all select '0001 ', 'Checkin ', 'F-2007 ', '2007-9-1 '
union all select '0001 ', 'Checkout ', 'F-2007 ', '2007-9-1 '
union all select '1045 ', 'Checkout ', 'X.Lt ', '2007-9-1 '
union all select '1045 ', 'Checkin ', 'G-/p ', '2007-9-1 '
union all select '1045 ', 'Checkout ', 'G-/p ', '2007-9-2 '
create function getX(@id varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
select @s=isnull(@s+ '. ', ' ')+number from a where id=@id
return @s
end
go
select
id,
checkin=sum(case when action= 'checkin ' then 1 else 0 end ),
checkin_number=min(case when action= 'checkin ' then dbo.getX(id) end),
checkout=sum(case when action= 'checkout '