求sql统计查询语句
ID FBILLNO num fdate
1 lxm12010001 12 2012-1-1
1 lxm12010001 13 2012-1-1
1 lxm12010001 10 2012-1-1
2 lxm12010002 50 2012-1-5
2 lxm12010002 60 2012-1-5
3 lxm12010003 10 2012-1-6
4 lxm12010004 10 2012-1-7
id、fbillno、fdate字段都是相对应的,我要查询出fbillno 字段大于2的记录显示出来
希望通过一条查询得到如下结果:
ID FBILLNO num fdate
1 lxm12010001 12 2012-1-1
1 lxm12010001 13 2012-1-1
1 lxm12010001 10 2012-1-1
2 lxm12010002 50 2012-1-5
2 lxm12010002 60 2012-1-5
[解决办法]
create table tb( ID int, FBILLNO varchar(20), num int , fdate varchar(10))insert into tbselect 1,'lxm12010001', 12, '2012-1-1' union allselect 1,'lxm12010001', 13, '2012-1-1' union all select 1,'lxm12010001', 10, '2012-1-1' union allselect 2,'lxm12010002', 50, '2012-1-5' union allselect 2,'lxm12010002', 60, '2012-1-5' union allselect 3,'lxm12010003', 10, '2012-1-6' union allselect 4,'lxm12010004', 10, '2012-1-7'Select * From tb Where FBILLNO In (Select FBILLNO From tb Group By FBILLNO Having Count(*)>1)ID FBILLNO num fdate----------- -------------------- ----------- ----------1 lxm12010001 12 2012-1-11 lxm12010001 13 2012-1-11 lxm12010001 10 2012-1-12 lxm12010002 50 2012-1-52 lxm12010002 60 2012-1-5
[解决办法]
select ID,FBILLNO,NUM,fdate from (Select *,(select COUNT(1) from tb where FBILLNO=t.FBILLNO )as co From tb t )s where co>1