34,587
社区成员
发帖
与我相关
我的任务
分享
declare @table1 table (id nvarchar(10),ip nvarchar(20),
[datetime] datetime,url nvarchar(20))
insert into @table1 select 2289,'219.143.139.241','2009-11-18 09:32:42.640','VFClassroom'
union all select 2290,'219.143.139.241','2009-11-18 09:32:56.043','KL_room'
union all select 2291,'219.143.139.241','2009-11-19 09:34:28.170','KL_room'
union all select 2292,'219.143.139.241','2009-11-19 09:57:20.750','Index.aspx'
union all select 2293,'219.143.139.241','2009-11-19 09:57:27.670','Index.aspx'
union all select 2294,'219.143.139.241','2009-11-19 09:57:32.327','Task'
union all select 2295,'219.143.139.241','2009-11-19 09:58:38.577','Index.aspx'
union all select 2296,'219.143.139.245','2009-11-19 09:58:51.793','Index.aspx'
union all select 2297,'219.143.139.245','2009-11-19 09:58:58.310','Task'
union all select 2298,'219.143.139.245','2009-11-19 09:59:22.170','Index.aspx'
declare @table2 table( id int identity(1,1) ,ip nvarchar(20),num int, [datetime] datetime)
select ip,时间=convert(nvarchar(10),datetime,121),num=count(1)
into # from @table1
group by ip,convert(nvarchar(10),datetime,121)
select ip,sum(num),[datetime] from
(select ip,num,[datetime] from @table2
union all
select ip,num,时间 from #) tb group by ip,datetime
-----把@table2 清空,把上面的数据插入
go
drop table #
/*
ip datetime
-------------------- ----------- -----------------------
219.143.139.241 2 2009-11-18 00:00:00.000
219.143.139.241 5 2009-11-19 00:00:00.000
219.143.139.245 3 2009-11-19 00:00:00.000
(3 行受影响)
*/
--一句搞不定~
--> 测试数据:@table1
create table #table1 ([id] int,[ip] varchar(15),[datetime] datetime,[url] varchar(11))
insert into #table1
select 2289,'219.143.139.241','2009-11-18 09:32:42.640','VFClassroom' union all
select 2290,'219.143.139.241','2009-11-18 09:32:56.043','KL_room' union all
select 2291,'219.143.139.241','2009-11-19 09:34:28.170','KL_room' union all
select 2292,'219.143.139.241','2009-11-19 09:57:20.750','Index.aspx' union all
select 2293,'219.143.139.241','2009-11-19 09:57:27.670','Index.aspx' union all
select 2294,'219.143.139.241','2009-11-19 09:57:32.327','Task' union all
select 2295,'219.143.139.241','2009-11-19 09:58:38.577','Index.aspx' union all
select 2296,'219.143.139.245','2009-11-19 09:58:51.793','Index.aspx' union all
select 2297,'219.143.139.245','2009-11-19 09:58:58.310','Task' union all
select 2298,'219.143.139.245','2009-11-19 09:59:22.170','Index.aspx'
create table #table2([id] int identity(1,1),[ip] varchar(15),[num] int,[datetime] varchar(10))
insert into #table2
select '219.143.139.241',4,'2009-11-18'
select * into #table3 from #table2
truncate table #table2
insert into #table2
select ip,sum(num) as num,datetime from
(select ip,count(1) as num,convert(varchar(10),datetime,120) as datetime
from #table1
group by ip,convert(varchar(10),datetime,120)
union
select ip,num,datetime from #table3) r
group by ip,datetime
select * from #table2
drop table #table1,#table2,#table3
select ip,num, datetime
into #temp2
from table2
select ip, count(*) as num, datetime
into #temp1
from table1
group by ip, datetime
truncate table table2
insert into table2( ip, num, datetime )
select a.ip,
sum(a.num) as num,
a.datetime
from ( select *
from #temp1
union all
select *
from #temp2
) a
group by a.ip, a.datetime
select ip,num, datetime
into #temp2
from table2
select ip, count(*) as num, datetime
into @temp1
from table1
group by ip, datetime
truncate table table2
insert into table2( ip, num, datetime )
select *
from ( select *
from #temp1
union all
select *
from temp2
) a
group by a.ip, a.datetime
select
declare @tb1 table([帐号] int,[姓名] varchar(1),[余额] int)
insert @tb1
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
declare @tb2 table([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert @tb2
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
update t2
set t2.累计积数 = t2.累计积数 + t1.余额,
t2.余额 = t1.余额
from @tb1 t1 inner join @tb2 t2 on t1.帐号 = t2.帐号
insert into @tb2(帐号, 姓名, 上次余额, 余额, 累计积数)
select t1.帐号, t1.姓名, t1.余额, t1.余额, t1.余额
from @tb1 t1 left join @tb2 t2 on t1.帐号 = t2.帐号
where t2.帐号 is null
select * from @tb2 order by 帐号
/*
帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 500 500 500
1002 B 100 300 1800
1003 C 300 100 400
(所影响的行数为 3 行)
update table2
set num = num + isnull((select count(1) from table1 where ip = t.ip and datetime = convert(varchar(10),t.datetime,120)),0)
from table2 t