这是原来的SQL统计语句
select UrlAddress,count(*) as ipCount from LeaveWordTongJi where datediff(day,addDate,getdate())=0 group by UrlAddress 这是表的机构
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KeyCounts]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KeyCounts]
GOCREATE TABLE [dbo].[KeyCounts] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[UrlAddress] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL ,
[HostIp] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL ,
[AddDate] [datetime] NULL 
) ON [PRIMARY]
GO
少量数据---------------------------  KeyCountsINSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (1,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=招商','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (2,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (3,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (4,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (5,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (6,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (7,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (8,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (9,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (10,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (11,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (12,'http://www.xxx.com/ZhaoShang/ZhaoShang.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (13,'http://www.xxx.com/ZhaoShang/Yahoo.aspx?key=远程','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (14,'http://www.xxx.com/ZhaoShang/Yahoo.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (15,'http://www.xxx.com/ZhaoShang/baidu.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')
INSERT INTO KeyCounts (ID,UrlAddress,HostIp,AddDate) VALUES (16,'http://www.xxx.com/ZhaoShang/28.aspx?key=我学','127.0.0.1','2008-05-26 14:18:23')我想把统计添加上AddDate字段显示出来!怎么实现 谢谢大家!

解决方案 »

  1.   

    select UrlAddress,AddDate,count(*) as ipCount from LeaveWordTongJi where datediff(day,addDate,getdate())=0 group by UrlAddress,AddDate
      

  2.   

    不是这样的 ,这样写 ipCount  值都是1的  
      

  3.   


    select UrlAddress,count(*) as ipCount,AddDate from LeaveWordTongJi where datediff(day,addDate,getdate())=0 group by UrlAddress,AddDate上面这种肯定不是LZ想要的结果。貌似你的逻辑有问题,你是想统计每个来源地址的数量和时间,显然有问题。你想三,统计的来源地址的数量和是1个值,但时间是多个值。
      

  4.   

    select UrlAddress,count(*) as ipCount, getdate() as AddDate from LeaveWordTongJi where datediff(day,addDate,getdate())=0 group by UrlAddress
      

  5.   

    嗯,相同的UrlAddress,其AddDate不同,你想匹配哪个AddDate
      

  6.   

    try:select UrlAddress,count(UrlAddress)ipCount,(select top 1 AddDate from 
    KeyCounts main where main.UrlAddress=UrlAddress ) from KeyCounts where datediff(day,addDate,getdate())=0 
    group by UrlAddress
      

  7.   

    非常抱歉,上面的有问题,用下面的吧(如果不想随即取一个时间那么就把order by newid()去掉)
    select UrlAddress,count(UrlAddress)ipCount,(select top 1 AddDate from 
    KeyCounts temp where temp.UrlAddress=main.UrlAddress order by newid())AddDate from 
    KeyCounts main where datediff(day,addDate,getdate())=0 
    group by UrlAddress
      

  8.   

    select UrlAddress,count(*) as ipCount,max(addDate) as AddDate
    from KeyCounts where datediff(day,addDate,getdate())=0 group by UrlAddress 取最大的时间
      

  9.   

    select UrlAddress,max(AddDate),count(*) as ipCount from LeaveWordTongJi where datediff(day,addDate,getdate())=0 group by UrlAddress