同一表中 不同类型的数据 按类型返回前n条數據?
比如
table
------------------------------
id | name | category | price
------------------------------
int| char | char | decimal
------------------------------
originalData:
------------------------------
0 | na | c1 | 10
1 | nb | c1 | 400
2 | nc | c1 | 400
3 | nd | c2 | 60
4 | ne | c2 | 40
5 | nf | c2 | 60
6 | ng | c2 | 200
7 | nh | c3 | 20
8 | ni | c3 | 80
9 | nj | c3 | 100
10 | nk | c3 | 100
expected:(n = 2)
------------------------------
1 | nb | c1 | 400
2 | nc | c1 | 400
6 | ng | c2 | 200
3 | nd | c2 | 60
9 | nj | c3 | 100
10 | nk | c3 | 100
我需要每个种类(category)里面;价钱(price)最高的n条数据.
怎样写查询语句?
期望结果是 种类数目x n 条(distict count(category))*2
------解决方案--------------------select T.*
from originalData T
where id in(select top 2 id from originalData where T.category=category order by price DESC)
------解决方案----------------------expected:(n = 3)
--条件语句 子查询 top 后面为n
select * from originaldata a
where a.id in ( select top 3 id from originaldata where category = a.category order by price desc )
order by category , price desc
--------------------------------
1 nb c1 400.00
2 nc c1 400.00
0 na c1 10.00
6 ng c2 200.00
3 nd c2 60.00
5 nf c2 60.00
9 nj c3 100.00
10 nk c3 100.00
8 ni c3 80.00
------解决方案--------------------create table origi