日期:2014-05-18  浏览次数:20403 次

请问这样的怎么做??急
ID   |   name   |   TEXT   |
                1   |   2         |   1
                2   |   2         |   1
                3   |   2         |   3
                4   |   2         |   3
                5   |   1         |   4
                6   |   1         |   4
                7   |   1         |   4
                8   |   1         |   3
要求结果
            name     |   text   |   textcount
              2         |     1       |   2        
              2         |     3       |   2  
              1         |     4       |   3
              1         |     3       |   1

------解决方案--------------------
create table tb(ID int,name int,TEXT int)
insert tb
select 1 , 2 , 1
union select 2 , 2 , 1
union select 3 , 2 , 3
union select 4 , 2 , 3
union select 5 , 1 , 4
union select 6 , 1 , 4
union select 7 , 1 , 4
union select 8 , 1 , 3


select name,text,textcount=count(*) from tb group by name,text order by name desc,textcount desc


drop table tb


/*
要求结果
name text textcount
----------- ----------- -----------
2 1 2
2 3 2
1 4 3
1 3 1

(4 row(s) affected)
*/
------解决方案--------------------
create table tb(ID int,name int, TEXT int)
insert into tb values(1 , 2 , 1)
insert into tb values(2 , 2 , 1)
insert into tb values(3 , 2 , 3)
insert into tb values(4 , 2 , 3)
insert into tb values(5 , 1 , 4)
insert into tb values(6 , 1 , 4)
insert into tb values(7 , 1 , 4)
insert into tb values( 8 , 1 , 3)
select name,text, count(*) textcount from tb group by name,text order by name desc

drop table tb