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

抽取不同的数据
SELECT   top   12   financial.effDate,financial.appDate,   price.fundName   ,  
price.bidPrice,   price.offerPrice,   financial.unitNum   ,financial.accUnitNum   *   price.offerPrice   AS   InvestPrice  
FROM   financial   INNER   JOIN   price   ON   financial.fundId   =   price.fundId  
WHERE   (financial.contractNo   =   'BB00001001 ')   and   (price.transCode= '00 '   )
order   by   financial.effDate   desc

在price.fundName   里面有
成长型,稳定型,犹豫期,安益型,避险型和平衡型这几种帐户
我现在查出来   price.fundName   下面都是属于   成长型账户
有没有方法能使这6个帐户都分别查出来2条
在上面的sql语句上面更改

谢谢了

------解决方案--------------------
分类取前N个记录的SQL语句

有一个表AAA,结构如下:
类别编号 说明 排序
a aa 1
a aa2 2
a aa3 3
b bb 1
b bb2 2
b bb3 3
c cc 1
c cc2 2
c cc3 3
需要查询出来的结果是每个类别的头2条记录,按排序进行排序,结果如下:
类别编号 说明 排序
a aa 1
a aa2 2
b bb 1
b bb2 2
c cc 1
c cc2 2
谢谢各位了!只要测试通过马上给分!


if object_id( 'pubs..t1 ') is not null
drop table t1
go

create table t1(
类别编号 varchar(10),
说明 varchar(10),
排序 int
)
insert into t1(类别编号,说明,排序) values( 'a ', 'aa ',1)
insert into t1(类别编号,说明,排序) values( 'a ', 'aa2 ',2)
insert into t1(类别编号,说明,排序) values( 'a ', 'aa3 ',3)
insert into t1(类别编号,说明,排序) values( 'b ', 'bb ',1)
insert into t1(类别编号,说明,排序) values( 'b ', 'bb2 ',2)
insert into t1(类别编号,说明,排序) values( 'b ', 'bb3 ',3)
insert into t1(类别编号,说明,排序) values( 'c ', 'cc ',1)
insert into t1(类别编号,说明,排序) values( 'c ', 'cc2 ',2)
insert into t1(类别编号,说明,排序) values( 'c ', 'cc3 ',3)

select * from t1 as t
where (select count(*) from t1 where 类别编号 = t.类别编号 and 排序 < t.排序) < 2

drop table t1


类别编号 说明 排序
---------- ---------- -----------
a aa 1
a aa2 2
b bb 1
b bb2 2
c cc 1
c cc2 2

(所影响的行数为 6 行)