请大家帮个忙

evonne0102 2008-01-16 09:26:16
现在有个表:A(id ,name,regdate), B(id,groupid),C(id,name2)
regdate注册日期(格式为2007/02/12)
Name姓名
写出下面的SQL语句
1).统计A表中每个月注册用户数
2).统计A表中有姓名相同的用户数
3).如果表A中有姓名相同的用户,把相同的查出,写入表C中
4).A中ID有多个相同的数据,A中姓名相同的ID只保留注册时间最大的数据
...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2008-01-16
  • 打赏
  • 举报
回复
1).统计A表中每个月注册用户数 
select convert(varchar(10),regdate,120) 年月 , count(*) 注册用户数 from A group by convert(varchar(10),regdate,120)

2).统计A表中有姓名相同的用户数
select count(*) 姓名相同的用户数 from
(
select name from a group by name having count(*) > 1
) t

3).如果表A中有姓名相同的用户,把相同的查出,写入表C中
insert into c(name) select name from a group by name having count(*) > 1

4).A中ID有多个相同的数据,A中姓名相同的ID只保留注册时间最大的数据
delete a from a t where regdate not in (select max(regdate) from a where name = t.name)

pt1314917 2008-01-16
  • 打赏
  • 举报
回复

--1)
select convert(varchar(7),regdate,120)[日期],count(1)[数量] from A group by convert(varchar(7),regdate,120)

--)2
select name,count(1)[数量] from A group by name

--3)
如果c表的id为自增的话:
insert into c select name from a group by name having count(1)>1

--4)
select * from A t where not exists(select 1 from A where name=a.name and regdate>a.regdate)
marco08 2008-01-16
  • 打赏
  • 举报
回复
考试题目?
liangCK 2008-01-16
  • 打赏
  • 举报
回复
4
select *
from tb a
where not exists(select 1 from tb
where id=a.id and name=a.name and regdate>a.regdate)
liangCK 2008-01-16
  • 打赏
  • 举报
回复
1
select convert(char(7),regdate,120) regdate,count(*) cnt
from tb
group by convert(char(7),regdate,120)

2
select name,count(*) cnt
from tb
group by name

3
insert into c
select name
from tb a
where exists(select 1 from tb where a.name=name and a.id>id)

34,571

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧