简体   繁体   English

配置单元查询遍历结果,选择顶部

[英]hive query iterate over results, choose top

I'm joining two tables, USER and TOP_ITEMS_BY_CATEGORY. 我要加入两个表,USER和TOP_ITEMS_BY_CATEGORY。 And my goal is an output which accomplishes the following: 我的目标是实现以下目标的输出:

  • display the user_id, and top sku_id (if the cat_id has one), then top sku_id of next cat_id, repeat. 显示user_id和顶部sku_id(如果cat_id有一个),然后显示下一个cat_id的顶部sku_id,重复。

Here are the tables: 表格如下:

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

Just a simple inner join will work. 只需简单的内部联接即可。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM