日期:2014-05-19  浏览次数:20400 次

求一个select语句
Table表结构有这几个字段:ID(唯一ID,GUID)   IP(IP地址)   AddDate(加人时间)   UID(用户名)

CREATE   PROCEDURE   ReportDay
@Date   datetime
AS
SELECT   *   FROM   Table   WHERE   DAY(adddate)=DAY(@Date)............

请大家给我写写这个select语句

要求出来的数据是@Date这天的数据,格式如下
点击量为一共记录了多少个次,有效IP为把点击量里面重复的IP去掉,只保留一个

UID(用户名)           点击量             有效IP


------解决方案--------------------
try

CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID, Count(*) As 点击量, Count(Distinct IP) As 有效IP FROM [Table] WHERE DateDiff(dd, adddate, @Date) = 0
GO
------解决方案--------------------
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID, Count(*) As 点击量, Count(Distinct IP) As 有效IP FROM [Table] WHERE DateDiff(dd, adddate, @Date) = 0
group by UID
------解决方案--------------------
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT
UID as 用户名,
count(*) as 点击量,
count(distinct ip) as 有效IP
FROM [Table]
WHERE convert(varchar(8),adddate,112)=convert(varchar(8),@date,112)
group by UID ---加这里

go

------解决方案--------------------

GROUP BY
------解决方案--------------------
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT
UID as 用户名,
count(ip) as 点击量,
ip as 有效IP
FROM [Table]
WHERE adddate=@date
group by UID,ip
go

------解决方案--------------------
暈,掉了group by了

CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID, Count(*) As 点击量, Count(Distinct IP) As 有效IP FROM [Table] WHERE DateDiff(dd, adddate, @Date) = 0 Group By UID
GO

------解决方案--------------------
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID, Count(*) As 点击量, Count(Distinct IP) As 有效IP FROM [Table] WHERE DateDiff(dd, adddate, @Date) = 0 Group By UID
GO
---------------------------------------------

一样的思路!
------解决方案--------------------
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID, Count(*) As 点击量, Count(Distinct IP) As 有效IP FROM [Table] WHERE DateDiff(dd, adddate, @Date) = 0 Group By UID
GO

可以
------解决方案--------------------
create table test(id int,[uid] varchar(10),[ip] varchar(16),[adddate] datetime)
insert into test select '1 ', 'aa ', '127.0.0.1 ', '2007-7-24 '
insert into test select '2 ', 'aa ', '127.0.0.1 ', '2007-7-24 '
insert into test select '3 ', 'aa ', '192.168.0.1 ', '2007-7-24 '
insert into test select '4 ', 'bb ', '127.0.0.1 ', '2007-7-24 '
insert into test select '5 ', 'bb ', '192.168.0.100 ', '2007-7-24 '
insert into test select '6 ', 'bb ', '192.168.0.99 ', '2007-7-24 '

drop proc ReportDay
CREATE PROCEDURE ReportDay
@Date datetime
AS
SELECT UID as 用户名, count(*) as 点击量, count(distinct ip) as 有效IP