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

关于一个查询的问题
表   TABLE1:
    A           B             C               D
    001       张三       100           2007-4-1
    002       李四       200           2007-4-2
    003       王五       300           2007-4-3
    001       张三       200           2007-4-5
    002       李四       300           2007-4-1


我想得到的结果是:(只要能出现以下的结果就行,也可以用临时表)最好是速度快的
    编号     姓名       出现的次数
    001       张三               2
    002       李四               2
    003       王五               1



------解决方案--------------------
select A as 编号,B as 姓名,count(*) as 出现次数
from table1
group by A,B
------解决方案--------------------
--这个给你做参考,注意不是答案,因为在最大的日期有两条以上记录时候,它会有问题
select t2.编号,t1.收费号,t2.时间,t2.次数
from table1 t1
join(
select 编号,count(*) 次数,Max(时间) as 时间
from table1
group by 编号
) t2 on t1.编号=t2.编号 and t1.时间=t2.时间

------解决方案--------------------
select t2.编号,t1.收费号,t2.时间,t2.次数
from table1 t1
inner join(
select 编号,count(*) 次数,Max(时间) as 时间
from table1
group by 编号
) t2 on t1.编号=t2.编号 and t1.时间=t2.时间
------解决方案--------------------
对于第一楼的这样应该就可以
select a,b,count(1) from (select a,b,c from table1 group by a,b,c) as xx group by a,b