34,590
社区成员
发帖
与我相关
我的任务
分享
insert into table2(ip,[time],[count])
select ip,[time],count(*) from table1 group by ip, [time]
create table table1 (id int, ip varchar(15), time varchar(10))
insert into table1 values(0 , '11.11' , '2005-02-13')
insert into table1 values(1 , '22.33' , '2005-02-13')
insert into table1 values(2 , '11.11' , '2005-02-13')
insert into table1 values(3 , '22.33' , '2005-02-13')
create table table2(id int identity(1,1),ip varchar(10), ipnum int,time varchar(10))
insert into table2(ip,ipnum,time)
select ip,count(ip),time as time from table1 group by ip,time
COUNT(1) 功能同于 COUNT(*),COUNT(ID) --如果ID建了聚集索引的话(指性能上)
这里意思是 按 a 分组后,每组的个数.
HAVING COUNT(1)>3 指此组的个数要大于3才被选出
insert into table2(ip,ipnum,time ) select ip , count(1) , time from table1 group by ip , time
create table table1 (id int, ip varchar(15), time varchar(10))
insert into table1 values(0 , '11.11' , '2005-02-13')
insert into table1 values(1 , '22.33' , '2005-02-13')
insert into table1 values(2 , '11.11' , '2005-02-13')
insert into table1 values(3 , '22.33' , '2005-02-13')
create table table2(id int,ip varchar(10), ipnum int,time varchar(10))
go
insert into table2 select min(id) , ip , count(1) , time from table1 group by ip , time
select * from table2
drop table table1, table2
/*
id ip ipnum time
----------- ---------- ----------- ----------
0 11.11 2 2005-02-13
1 22.33 2 2005-02-13
(所影响的行数为 2 行)
*/
insert into table2 select min(id) , ip , count(1) , time from table1 group by ip , time
--> 测试数据:table1
if object_id ('table1') is not null drop table table1
create table table1([id] int,[ip] numeric(4,2),[time] varchar(10))
insert table1
select 0,11.11,'2005-02-13' union all
select 1,22.33,'2005-02-13' union all
select 2,11.11,'2005-02-13' union all
select 3,22.33,'2005-02-13'
--> 测试数据:table2
if object_id ('table2') is not null drop table table2
create table table2([id] int,[ip] numeric(4,2),[ipnum] int,[time] varchar(10))
insert into table2
select min(id),ip,count(1),time
from table1
group by ip,time
select * from table2
--结果
----------------------
0 11.11 2 2005-02-13
1 22.33 2 2005-02-13
insert into tb2
select min(id),ip,count(*),time from tb1
group by ip,time
update t2
set t2.ipnum=t2.ipnum+isnull(t1.cnt,0)
from
table2 t2
left join
(select ip,convert(varchar(10),[time],120) as [time],count(1) as cnt from table1 group by ip,convert(varchar(10),[time],120)) t1
on
t1.ip=t2.ip and t1.[time]=convert(varchar(10),t2.[time],120)