问题
I have one table with the current state_id of my customers and another table holding all states and their state_ids, but without the corresponding customer_id. However, the historical state table holds the information of which state_id it replaced. Hence, it should be possible to recursively track the states/journey of the customer.
Consider the following example:
"Customer" table:
customer_id state_created current_state_id
1 2017-11-09 33
2 2018-04-01 243
3 2018-07-10 254
"Historical_state" table:
state_name replace_state_id state_id state_created
State1 22 2015-10-08
State1 211 2017-06-28
State3 254 2018-07-10
State4 211 243 2018-04-01
State5 22 33 2017-11-09
I am interested in getting the historical state information on every customer, ie. the following table:
customer_id state_created state_name
1 2015-10-00 State1
1 2017-11-09 State5
2 2017-06-28 State1
2 2018-04-01 State4
3 2018-07-10 State3
The data is replaced in AWS' Athena, so the presto sql should be used as the language.
回答1:
Here is one option using a union. The crux behind this query is that we generate a single logical state_id
column to which we join the customer table. This table contains both current and replace state_id
values for a given state.
SELECT
c.customer_id,
t.state_created,
t.state_name
FROM Customer c
INNER JOIN
(
SELECT state_id, state_name, state_created
FROM Historical_state
UNION ALL
SELECT h1.state_id, h2.state_name, h2.state_created
FROM Historical_state h1
INNER JOIN Historical_state h2
ON h1.replace_state_id = h2.state_id
) t
ON c.current_state_id = t.state_id;
Here is a demo in MySQL, because Rextester does not support SQLite, but it at least shows that the query logic is correct.
Demo
来源:https://stackoverflow.com/questions/51823728/recursively-tracking-state-of-customers-presto-sql