sql求答

wt0625 2009-03-16 09:35:53
表C,字段id integer,name varchar(20),
regdate date
表A(id,name2);
问:
a,统计C表中每月注册用户数
b,统计C表中有姓名相同用户数
c,如C中有姓名相同的用户,把相同的查出写入表A中
d,C中ID有多个相同数据,C中姓名相同的ID只保留注册时间最大数据
...全文
60 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wt0625 2009-03-16
  • 打赏
  • 举报
回复
肯定会加分的
htl258_Tony 2009-03-16
  • 打赏
  • 举报
回复
--a. 
select year(regdate) 注册年份,month(regdate) 注册月份,count(*) 用户数 from C group by year(regdate),month(regdate) --考虑到跨年
--b.
select count(distinct name) from C t where exists(select 1 from C where name=t.name and id <t.id)
--c.
insert A select id,name from C t where exists(select 1 from C where name=t.name and id <t.id)
--d.
select * from C t where not exists(select 1 from C where name=t.name and regdate>t.regdate)
htl258_Tony 2009-03-16
  • 打赏
  • 举报
回复
--首先我认为楼主是描述错了,认为ID是唯一的,以下处理按ID维一的来处理。如果ID不是维一的,则只需加临时表处理。select fid=identiry(int,1,1),* into #C from C
--a.
select year(regdate) 注册年份,month(regdate) 注册月份,count(*) 用户数 from C group by year(regdate),month(regdate) --考虑到跨年
--b.
select count(distinct name) from C t where exists(select 1 from C where name=t.name and id<t.id)
--c.
if exists(select 1 from C t where exists(select 1 from C where name=t.name and id<t.id))
insert A select id,name from C t where exists(select 1 from C where name=t.name and id<t.id)
--d.
select * from C t where not exists(select 1 from C where name=t.name and regdate>t.regdate)

楼主问题要解决了要记得补加分啊。

22,181

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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