现在有个表:A(id ,name,regdate)
           B(id,groupid)
           C(id,name2)
写出下面的SQL语句
1.统计A表中每个月注册用户数
2.统计A表中有姓名相同的用户数
3.如果表A中有姓名相同的用户,把相同的查出,写入表C中
4.A中ID有多个相同的数据,A中姓名相同的ID只保留注册时间最大的数据
大家帮帮忙,写一下吧

解决方案 »

  1.   

    1
    select count(*),to_char(regdate,'yyyymm') from A group by to_char(regdate,'yyyymm');
    2
    select count(*) from (select name from A group by name having count(*) >1);

    insert into C(name2) select name from A group by name having count(*) >1;
    4
    delete from A E where e.regdate < (select max(regdate) from a X where E.id = X.id);------
    redate(格式为2007/02/12)日期自己转化一下
    SQL> select to_date('2007/02/12','yyyy/mm/dd') from dual;TO_DATE('2007/02/12','YYYY/MM/
    ------------------------------
    2007-2-12
      

  2.   

    第二句应为
    select count(*) as 姓名相同用户总数 from (select name from A group by name having count(*)>1)  as a
      

  3.   

    2:
    select name,count(id) from a
    group by name
      

  4.   

    3.
       ocursor  i_ocursor is 
         select id,name from  A  group  by  nane  having count(*)>1;                                               
     FETCH i_ocursor INTO  
                  t_id,
                  t_name;
    WHILE  i_ocursor%FOUND LOOP 
        insert into c  values ("t_id","t_name");
        FETCH i_ocursor INTO  
                  t_id,
                  t_name; 
       END LOOP;                                                                       
      CLOSE cur_tel;