表中数据如下:
id  name  date
1   a     2007-4-13
2   b     2007-4-13
3   c     2007-4-13
4   d     2007-4-13
1   a     2007-4-14
1   a     2007-4-15
2   b     2007-4-14希望查询结果如下:
id  name  sum  date
1   a     3     2007-4-13
1   a     3     2007-4-14
1   a     3     2007-4-15
2   b     2     2007-4-13
2   b     2     2007-4-14
3   c     1     2007-4-13
4   d     1     2007-4-13谢谢!

解决方案 »

  1.   

    select 
       a.id,
       a.name,
       b.[sum],
       a.date
    from 
       表 a,
       ( 
          select 
             id,
             count(*) as [sum]
          from 
             表
          group by 
             id
        ) as b
    where
        a.id=b.id
    order by
        a.id,
        a.date
      

  2.   

    or:
    select 
       id,
       name,
       [sum]=(select count(*) from 表 where id=a.id),
       date
    from 
       表 a
    order by
        id,
        date
      

  3.   

    原表的字段 分别为tid,tname,tdateselect tb.* ,t.consum from tb join (select tname ,count(*)consum from tb group by tname) t
    on tb.tname=t.tname order by consum desc,tdate
      

  4.   

    Select * , consum=(select count(*) from tb where id=a.id)
     from tb as a Order by Consum desc,tdate
      

  5.   

    create table #t (id int, name varchar(100), [date] datetime)insert into #t
    select 1,'a','2007-4-13'  union all
    select 2,'b','2007-4-13'  union all
    select 3,'c','2007-4-13'  union all
    select 4,'d','2007-4-13'  union all
    select 1,'a','2007-4-14'  union all
    select 1,'a','2007-4-15'  union all
    select 2,'b','2007-4-14'
    select [id],[name],(select count(*) from #t where id=a.id) as [sum],[date]
    from #t a
    order by [id],[date]drop table #t
      

  6.   

    create table tt(id varchar(10),name varchar(10),date datetime)insert tt select '1','a','2007-4-13'
    union all select '2','b','2007-4-13'
    union all select '3','c','2007-4-13'
    union all select '4','d','2007-4-13'
    union all select '1','a','2007-4-14'
    union all select '1','a','2007-4-15'
    union all select '2','b','2007-4-14'select id,name,sum=(case id when id then count(id) end)
    ,date from tt group by name,id,date order by id,nameselect * into #t from (select name,count(name) as ct from tt a group by name)bselect * from #tselect id,#t.name,#t.ct,date from tt,#t
    where tt.name=#t.name order by id,tt.name,tt.datedrop table #t
    drop table tt(所影响的行数为 7 行)id         name       sum         date                                                   
    ---------- ---------- ----------- ------------------------------------------------------ 
    1          a          1           2007-04-13 00:00:00.000
    1          a          1           2007-04-14 00:00:00.000
    1          a          1           2007-04-15 00:00:00.000
    2          b          1           2007-04-13 00:00:00.000
    2          b          1           2007-04-14 00:00:00.000
    3          c          1           2007-04-13 00:00:00.000
    4          d          1           2007-04-13 00:00:00.000
      

  7.   

    贴错了
    (所影响的行数为 4 行)id         name       ct          date                                                   
    ---------- ---------- ----------- ------------------------------------------------------ 
    1          a          3           2007-04-13 00:00:00.000
    1          a          3           2007-04-14 00:00:00.000
    1          a          3           2007-04-15 00:00:00.000
    2          b          2           2007-04-13 00:00:00.000
    2          b          2           2007-04-14 00:00:00.000
    3          c          1           2007-04-13 00:00:00.000
    4          d          1           2007-04-13 00:00:00.000