我有一个非常大的数据库,看起来像这样(3米行)
+-----------+----------------+-------+
| Category | SubCategory | Count |
+-----------+----------------+-------+
| cat1 | green | 200 |
| cat1 | blue | 215 |
| cat1 | red | 20 |
| cat2 | something | 540 |
| cat2 | chicken | 100 |
| cat2 | dog | 33 |
| cat3 | ball | 404 |
| cat3 | bed | 777 |
| cat4 | shoes | 72 |
| cat4 | green | 14 |
| cat4 | red | 2 |
| cat4 | panda | 4 |
+-----------+----------------+-------+
如何按Count排序然后获取cat1的前100个结果,然后获取cat2的前100个结果,依此类推。
答案 0 :(得分:1)
您可以通过内部联接和分组来执行此操作。
注意:我将count
更改为count1
。在查询中不使用db保留关键字始终是一种好习惯。
的 SQLFiddle Demo
强>
SELECT category,
subcategory,
count1
FROM
(SELECT t1.category,
max(t1.subcategory) AS subcategory,
max(t1.count1) AS count1,
count(t1.count1) AS rnk
FROM tbl t1
INNER JOIN tbl t2 ON t1.category=t2.category
AND t1.count1<=t2.count1
GROUP BY t1.category,
t1.count1) cnt
WHERE rnk <=100
内部查询的工作方式与rank over partition by
类似,并rank
按count1 desc
分组category
。它的输出类似于
+----------+-------------+--------+-----+
| category | subcategory | count1 | rnk |
+----------+-------------+--------+-----+
| cat1 | red | 20 | 3 |
| cat1 | green | 200 | 2 |
| cat1 | blue | 215 | 1 |
| cat2 | dog | 33 | 3 |
| cat2 | chicken | 100 | 2 |
| cat2 | something | 540 | 1 |
然后你可以通过外部查询获得前100行。
答案 1 :(得分:0)
您需要为每个组建立row_number
,然后相应地进行过滤。使用mysql
,您需要使用user-defined variables
:
select category, subcategory, `count`
from (
select *,
(
case category
when @curCategory
then @curRow := @curRow + 1
else @curRow := 1 AND @curCategory := category end
) + 1 AS rank
from yourtable cross join (select @curRow:=0, @curCategory:='') c
order by category, `count` desc
) t
where rank <= 100