我要加入两个表,USER和TOP_ITEMS_BY_CATEGORY。我的目标是实现以下目标的输出:
表格如下:
USER
user_id | cat_id | rank
a123 | c123 | 1
a123 | c234 | 2
a123 | c345 | 3
a789 | c789 | 1
a789 | c123 | 2
TOP_ITEMS_BY_CATEGORY
cat_id | sku_id | rank
c123 | s_123 | 1
c123 | s_234 | 2
c234 | s_345 | 1
c345 | s_456 | 1
c789 | s_567 | 1
c789 | s_678 | 2
How I'd want the OUTPUT:
OUTPUT
user_id | sku_id
a123 | s_123
a123 | s_345
a123 | s_456
a123 | s_234
a789 | s_567
a789 | s_123
a789 | s_678
a789 | s_234
最佳答案
只需简单的内部联接即可。
Select USER.USER_ID, TOP_ITEMS_BY_CATEGORY.sku_id
from USER
INNER JOIN TOP_ITEMS_BY_CATEGORY
ON USER.cat_id = TOP_ITEMS_BY_CATEGORY.cat_id
ORDER BY USER.USER_ID, TOP_ITEMS_BY_CATEGORY.RANK, USER.RANK;
关于sql - 配置单元查询遍历结果,选择顶部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34334899/