【发布时间】:2021-03-14 10:41:02
【问题描述】:
我正在尝试构建一个表格,该表格将根据当前的产品组合我可以销售的所有产品。
产品状态表
+-------------+--------------+----------------+
| customer_id | product_name | product_status |
+-------------+--------------+----------------+
| 1 | A | Active |
| 2 | B | Active |
| 2 | C | Active |
| 3 | A | Cancelled |
+-------------+--------------+----------------+
现在我正在尝试使用硬代码表进行交叉连接,该表将根据我们产品组合中的所有 4 种产品以及我想申请的状态为每个 customer_id 提供 4 行。
投资组合表
+--------------+------------+----------+
| product_name | status_1 | status_2 |
+--------------+------------+----------+
| A | Inelegible | Inactive |
| B | Inelegible | Inactive |
| C | Ineligible | Inactive |
| D | Inelegible | Inactive |
+--------------+------------+----------+
在我的代码中,我尝试使用 CROSS JOIN 来实现每个 customer_id 4 行。不幸的是,对于拥有不止一种产品的客户,我有两排/三排。
这是我的代码:
SELECT
p.customer_id,
CASE WHEN p.product_name = pt.product_name THEN p.product_name ELSE pt.product_name END AS product_name,
CASE
WHEN p.product_name = pt.product_name THEN p.product_status
ELSE pt.status_1
END AS product_status
FROM
products AS p
CROSS JOIN
portfolio as pt
这是我当前的输出:
+----+-------------+--------------+----------------+
| # | customer_id | product_name | product_status |
+----+-------------+--------------+----------------+
| 1 | 1 | A | Active |
| 2 | 1 | B | Inelegible |
| 3 | 1 | C | Inelegible |
| 4 | 1 | D | Inelegible |
| 5 | 2 | A | Ineligible |
| 6 | 2 | A | Ineligible |
| 7 | 2 | B | Active |
| 8 | 2 | B | Ineligible |
| 9 | 2 | C | Active |
| 10 | 2 | C | Ineligible |
| 11 | 2 | D | Ineligible |
| 12 | 2 | D | Ineligible |
| 13 | 3 | A | Cancelled |
| 14 | 3 | B | Ineligible |
| 15 | 3 | C | Ineligible |
| 16 | 3 | D | Ineligible |
+----+-------------+--------------+----------------+
如您所见,对于 customer_id 2,每个产品有两行,产品 B 和 C 的状态与 product_status 表中的状态不同。
在这种情况下,我想要实现的是一个有 12 行的表,其中显示了 product_status 表中的当前产品/状态,其余的产品/状态来自投资组合表已添加。
预期输出
+----+-------------+--------------+----------------+
| # | customer_id | product_name | product_status |
+----+-------------+--------------+----------------+
| 1 | 1 | A | Active |
| 2 | 1 | B | Inelegible |
| 3 | 1 | C | Inelegible |
| 4 | 1 | D | Inelegible |
| 5 | 2 | A | Ineligible |
| 6 | 2 | B | Active |
| 7 | 2 | C | Active |
| 8 | 2 | D | Ineligible |
| 9 | 3 | A | Cancelled |
| 10 | 3 | B | Ineligible |
| 11 | 3 | C | Ineligible |
| 12 | 3 | D | Ineligible |
+----+-------------+--------------+----------------+
不确定 CROSS JOIN 是否是最佳选择,但现在我的想法已经不多了。
【问题讨论】:
-
预期输出 ??
-
@Srinivas 我使用的是纯蜂巢。不火花
标签: hive hiveql cross-join