如何两次加入表格

时间:2018-08-13 16:13:57

标签: sql snowflake

我有以下两个表,我想将它们合并成第三个表

表A

customer_id first_order_date last_order_date
123          2017-04-06        2018-07-30
456          2017-08-07        2018-07-24
789          2018-03-13        2018-07-03

表B

order_id customer_id order_created_at num_of_products num_of_x_products
abc       123         2017-04-06           4              2
xyz       123         2018-07-30           5              3
def       456         2017-08-07           6              6
lmn       456         2018-07-24           4              1
ghi       789         2018-03-13           6              5
pqr       789         2018-07-03           3              3

我想加入这两个表并创建第三个表。我该怎么办?

customer_id first_order_date last_order_date first_num_of_products last_num_of_products
123           2017-04-06        2018-07-30        4         5
456           2017-08-07        2018-07-24        6         4

这是我的代码

 SELECT
          "cus".customer_id
          ,"fo".order_created_at as first_order_created_at
          ,"fo".order_id as first_order_id
          ,"fo".number_of_products as first_number_of_products   
          ,"lo".order_created_at as last_order_created_at
          ,"lo".order_id as last_order_id
          ,"lo".number_of_products as last_number_of_products

        FROM table_a AS "cus"
        INNER JOIN table_b AS "fo" 
        ON "cus".customer_id = "fo".customer_id
        AND "cus".first_order_date = "fo".order_created_at
        INNER JOIN table_b AS "lo" 
        ON "cus".customer_id = "lo".customer_id
        AND "cus".last_order_date = "lo".order_created_at

1 个答案:

答案 0 :(得分:0)

根据您的示例数据和预期结果,我认为您不需要使用JOIN,您只需要编写这样的查询即可。

尽管此示例是SQL服务器,但是snowflake支持row_number函数和 window函数

JOINt1.order_created_at = t2.first_order_datet1.order_created_at = t2.last_order_datet1.customer_id = t2.customer_id的子查询中的tabelA和tableB

结果将只得到first_order_datelast_order_date,因此请使用Row_number将行号加order_created_at

  1. rn=1的意思是first_order_date
  2. rn=2的意思是last_order_date

使用条件汇总函数

CREATE TABLE TableA(
  customer_id INT,
  first_order_date DATE,
  last_order_date DATE
);


insert into TableA values (123,'2017-04-06','2018-07-30');
insert into TableA values (456,'2017-08-07','2018-07-24');
insert into TableA values (789,'2018-03-13','2018-07-03');


CREATE TABLE TableB(
  customer_id INT,
  order_created_at DATE,
  num_of_products INT,
  num_of_x_products INT
);


INSERT INTO TableB VALUES ( 123,'2017-04-06',4,2);
INSERT INTO TableB VALUES ( 123,'2018-07-28',15,13);
INSERT INTO TableB VALUES ( 123,'2018-07-30',5,3);
INSERT INTO TableB VALUES ( 456,'2017-08-07',6,6);
INSERT INTO TableB VALUES ( 456,'2018-07-24',4,1);
INSERT INTO TableB VALUES ( 789,'2018-03-13',6,5);
INSERT INTO TableB VALUES ( 789,'2018-07-03',3,3);

查询1

SELECT customer_id,
       MIN(order_created_at) first_order_date,
       MAX(order_created_at) last_order_date, 
       MAX(CASE WHEN rn = 1 THEN num_of_products END) first_num_of_products,
       MAX(CASE WHEN rn = 2 THEN num_of_products END) last_num_of_products
FROM 
(
  select t1.*,ROW_NUMBER() OVER(PARTITION BY t1.customer_id ORDER BY t1.order_created_at) rn
  from TableB t1 
  INNER JOIN TableA t2 ON 
  (t1.customer_id = t2.customer_id) AND
  (t1.order_created_at = t2.first_order_date OR t1.order_created_at = t2.last_order_date)

) t1
WHERE t1.customer_id in (123,456)
GROUP BY t1.customer_id

Results

| customer_id | first_order_date | last_order_date | first_num_of_products | last_num_of_products |
|-------------|------------------|-----------------|-----------------------|----------------------|
|         123 |       2017-04-06 |      2018-07-30 |                     4 |                    5 |
|         456 |       2017-08-07 |      2018-07-24 |                     6 |                    4 |