导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

请大家帮我看看这个应该怎么写

basaka1982 2008-01-06 02:58:40
name date rel
A 2007-1-1 AA
A 2007-1-2 AA
A 2007-1-2 AA
A 2007-1-3 AA
B 2007-1-1 bb
B 2007-1-1 bb
B 2007-1-2 bb
c 2007-1-1 cc
c 2007-1-1 cc
根据上面的数据。得出下面的结果。就是查某一个人的date数

A 3
b 2
c 1

另外问下各位朋友。如何写好SQL语句。我现在只会写些简单的查询。稍微复杂点的就不知道如何下手了。看着别人写的总觉得好复杂。 请大家给点指点
...全文
44 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JL99000 2008-01-07
if object_id('tb') is not null
drop table tb
go
CREATe TABLE tb(name varchar(5),date datetime, rel varchar(10))
insert into tb select 'A','2007-1-1','AA'
insert into tb select 'A','2007-1-2','AA'
insert into tb select 'A','2007-1-2','AA'
insert into tb select 'A','2007-1-3','AA'
insert into tb select 'B','2007-1-1','bb'
insert into tb select 'B','2007-1-1','bb'
insert into tb select 'B','2007-1-2','bb'
insert into tb select 'c','2007-1-1','cc'
insert into tb select 'c','2007-1-1','cc'
select A.name,count(A.date) as 日期数 from (select distinct date,name from tb) A
group by A.name
回复
JavCof 2008-01-06
SELECT [name], COUNT(DISTINCT [datetime]), [rel]
FROM TAB_NAME
GROUP BY [name]
回复
pt1314917 2008-01-06

declare @t table(name varchar(5),date datetime, rel varchar(10))
insert into @t select 'A','2007-1-1','AA'
insert into @t select 'A','2007-1-2','AA'
insert into @t select 'A','2007-1-2','AA'
insert into @t select 'A','2007-1-3','AA'
insert into @t select 'B','2007-1-1','bb'
insert into @t select 'B','2007-1-1','bb'
insert into @t select 'B','2007-1-2','bb'
insert into @t select 'c','2007-1-1','cc'
insert into @t select 'c','2007-1-1','cc'

select name,count(distinct date) from @t group by name
回复
LIHY70 2008-01-06

select name,count(distinct date) from table group by name
试试
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告