比如统计表结构为:
id 时间 ip
1 2007-06-12:11.11.11 127.0.0.1
2 2007-06-12:11.10.11 127.0.0.1
3 2007-06-11:11.11.11 127.0.0.1
4 2007-06-11:11.10.11 127.0.0.1如何写查询结果为以下的语句:日期 数量
2007-06-12 2
2007-06-11 2
id 时间 ip
1 2007-06-12:11.11.11 127.0.0.1
2 2007-06-12:11.10.11 127.0.0.1
3 2007-06-11:11.11.11 127.0.0.1
4 2007-06-11:11.10.11 127.0.0.1如何写查询结果为以下的语句:日期 数量
2007-06-12 2
2007-06-11 2
Select
Convert(Varchar(10), 时间, 120) As 日期,
Count(ip) As 数量
From
统计表
Group By
Convert(Varchar(10), 时间, 120)
Order By
日期 Desc
from 表 group by convert(varchar(30),时间, 102)
--如果是字符型Select
Left( 时间, 10) As 日期,
Count(ip) As 数量
From
统计表
Group By
Left( 时间, 10)
Order By
日期 Desc
Create Table 统计表
(id Int,
时间 Varchar(20),
ip Varchar(10))
Insert 统计表 Select 1, '2007-06-12:11.11.11', '127.0.0.1'
Union All Select 2, '2007-06-12:11.10.11', '127.0.0.1'
Union All Select 3, '2007-06-11:11.11.11', '127.0.0.1'
Union All Select 4, '2007-06-11:11.10.11', '127.0.0.1'
GO
Select
Left( 时间, 10) As 日期,
Count(ip) As 数量
From
统计表
Group By
Left( 时间, 10)
Order By
日期 Desc
GO
Drop Table 统计表
--Result
/*
日期 数量
2007-06-12 2
2007-06-11 2
*/