如何连接具有 2 个不同 id 的 3 个表(雪花中的 sql)?

发布于 2025-01-11 07:15:14 字数 2328 浏览 0 评论 0原文

首先,我需要从一个表中解压缩数据以转换为一个新表。其次,我需要根据客户 ID Z 将另一个表连接到这个新表。第三,我需要使用不同的客户 ID(客户 ID Y)将另一个表连接到第二个表。

第 1 部分:我从表中获取此数据下面一个。

名称(第 1 列)值(第 2 列)Customer_idZ(第 3 列)
account_statusACTIVE1234
card_template奖励1234
customer_creation_date1/8/20221234
enroll_store_codepxweb1234
enroll_store_name网站1234
Push_opt_in1234

我正在转换表 A 中的数据到一个新表,格式如下,代码如下:

'''SELECT
Customer_idZ,
MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
 FROM Table A
GROUP BY Customer_idZ;
Customer_idZaccount_statuscard_templatecustomer_creation_dateenroll_store_codeenroll_store_namepush_opt_in
1234ACTIVEPX Rewards1/8/2022pxwebWeb SiteYes

第 2 部分:然后我想将另一个表(表 B)加入到这个新格式化的表中基于共享 customer_idZ 字段的数据。表B中的数据如下;

Customer_idZ状态Customer_idY
1234ACTIVE567890

第 3 部分:我需要根据共享的 Customer_idY 字段将另一个表(表 C)连接到表 B。表C中的数据如下;

Customer_idYHousehold_sizeChildren_present_in_household
5678906

最终:我只需要最终输出作为 customer_idY 和 customer_idZ 的列表。

我不确定如何构建三个表之间所需的多连接,并且需要为表 a 中具有“活动状态”且家庭规模为 4 人或以上的配置文件提取 id

First, I need to unzip data from one table to transform into a new table. Secondly, I need to join another table to this new table based on customer id Z. Thirdly, I need to join yet another table to that second table using a different customer id, customer id Y.

Part 1: I have this data from Table A below.

NAME (Column 1)VALUE (Column 2)Customer_idZ (Coulmn 3)
account_statusACTIVE1234
card_templateRewards1234
customer_creation_date1/8/20221234
enroll_store_codepxweb1234
enroll_store_nameWeb Site1234
push_opt_inYes1234

I am transforming this data from Table A into a new table formatted as the below with this code

'''SELECT
Customer_idZ,
MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
 FROM Table A
GROUP BY Customer_idZ;
Customer_idZaccount_statuscard_templatecustomer_creation_dateenroll_store_codeenroll_store_namepush_opt_in
1234ACTIVEPX Rewards1/8/2022pxwebWeb SiteYes

Part 2: I want to then join another table (Table B) to this this newly formatted data based on the shared customer_idZ field. The data in Table B is below;

Customer_idZStatusCustomer_idY
1234ACTIVE567890

Part 3: I need to join another table (Table C) to Table B based on the shared Customer_idY field. The data in Table C is below;

Customer_idYHousehold_sizeChildren_present_in_household
5678906Yes

Final: I need the final output solely to be a list of customer_idY and customer_idZ.

I am not sure how to structure the multi-join needed between the three tables and need to pull ids for those profiles that have active status' in table a and have household sizes of 4 or more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2025-01-18 07:15:14

我刚刚通过 @xQbert 更改了查询中的列名称,希望它有所帮助。

SELECT A.Customer_idZ
     , B.Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM (SELECT
      Customer_idZ,
      MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
      MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
      MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
      MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
      MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
      MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
      FROM  A
      GROUP BY Customer_idZ) A
INNER JOIN B
 on A.Customer_idZ = B.Customer_idZ
INNER JOIN C
 on B.Customer_idY = C.Customer_idY;

I just changed the column names in the query by @xQbert and hope it helps.

SELECT A.Customer_idZ
     , B.Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM (SELECT
      Customer_idZ,
      MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
      MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
      MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
      MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
      MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
      MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
      FROM  A
      GROUP BY Customer_idZ) A
INNER JOIN B
 on A.Customer_idZ = B.Customer_idZ
INNER JOIN C
 on B.Customer_idY = C.Customer_idY;
冷心人i 2025-01-18 07:15:14

我正在使用通用表表达式(CTE)“NEWA”来重新格式化您的“A”表...我们可以将其设为内联视图,但我发现 CTE 更易于阅读

我们可能需要更改 INNER JOIN< /code> 到 LEFT OUTER JOIN 取决于当相关记录不在关联表中时您想要如何处理...

With NewA As (SELECT
Customer_idZ,
MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
 FROM Table A
GROUP BY Customer_idZ)

SELECT Cusotmer_idZ
     , Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM NewA A
INNER JOIN B
 on A.Customer_idZ= B.Customer_idZ
INNER JOIN C
 on B.Customer_IDY = C.Customer_IDY
WHERE Household_size >= 4
  and account_status='Active'

然后,我们使用常见的联接语法来联接表之间的关系。

现在,如果您想要 A 中的所有记录,并且只需要 B 中的匹配记录,并且 A 中的所有记录组合 b 和 C 中的相关记录,那么我们可以进行这些外联接

。或者我们可以使用完整外联接返回所有表中的所有记录。只是取决于你想要什么。

我们还可以使用内联视图代替 CTE:

SELECT A.Cusotmer_idZ
     , B.Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM (SELECT
      Customer_idZ,
      MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
      MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
      MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
      MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
      MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
      MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
      FROM Table A
      GROUP BY Customer_idZ) A
INNER JOIN B
 on A.Customer_idZ= B.Customer_idZ
INNER JOIN C
 on B.Customer_IDY = C.Customer_IDY
WHERE Household_size >= 4
  and account_status='Active'

I'm ussing a common table expression (CTE) "NEWA" to reformat your "A" table... we could make this an inline view but I find a CTE easier to read

We may need to change INNER JOIN to LEFT OUTER JOIN depending on how you want to handle when related records are not in associated tables...

With NewA As (SELECT
Customer_idZ,
MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
 FROM Table A
GROUP BY Customer_idZ)

SELECT Cusotmer_idZ
     , Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM NewA A
INNER JOIN B
 on A.Customer_idZ= B.Customer_idZ
INNER JOIN C
 on B.Customer_IDY = C.Customer_IDY
WHERE Household_size >= 4
  and account_status='Active'

We then use common join syntax to join on the relationships between the tables.

Now we could make these outer joins if you want all records from A and only those that match from B and all records from A combine b and those related records in C.

or we could use full outer joins to return all records from all tables. just depends on what you're after.

We could also use an inline view instead of a CTE:

SELECT A.Cusotmer_idZ
     , B.Customer_idY
     , account_status
     , card_template
     , customer_creation_date
     , enroll_store_code
     , enroll_store_name
     , push_opt_in
     , Household_size
     , Children_present_in_household
FROM (SELECT
      Customer_idZ,
      MAX(CASE WHEN Name = 'account_status' THEN VALUE END) AS account_status,
      MAX(CASE WHEN Name = 'card_template' THEN VALUE END) AS card_template,
      MAX(CASE WHEN Name = 'customer_creation_date' THEN VALUE END) AS customer_creation_date,
      MAX(CASE WHEN Name = 'enroll_store_code' THEN VALUE END) AS enroll_store_code,
      MAX(CASE WHEN Name = 'enroll_store_name' THEN VALUE END) AS enroll_store_name,
      MAX(CASE WHEN Name = 'push_opt_in' THEN VALUE END) AS push_opt_in
      FROM Table A
      GROUP BY Customer_idZ) A
INNER JOIN B
 on A.Customer_idZ= B.Customer_idZ
INNER JOIN C
 on B.Customer_IDY = C.Customer_IDY
WHERE Household_size >= 4
  and account_status='Active'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文