如何使用SQL将表转换为节点对

时间:2016-12-19 19:10:12

标签: sql oracle etl transpose

我有一个针对用户点击构建的数据库。每当有人从网站点击访问另一个网站时,我会捕获用户名,网站以及点击路径中的点击次数。它看起来像这样:

+----------+-------+---------------+
| Customer | Click | Website       |
+----------+-------+---------------+
| john     | 1     | facebook      |
+----------+-------+---------------+
| john     | 2     | microsoft     |
+----------+-------+---------------+
| john     | 3     | facebook      |
+----------+-------+---------------+
| steven   | 1     | apple         |
+----------+-------+---------------+
| steven   | 2     | stackoverflow |
+----------+-------+---------------+
| steven   | 3     | cnn           |
+----------+-------+---------------+  

我想想象一下人们如何在互联网上四处走动。我相信节点路径是最好的想法,但我不知道如何将这些数据转换成它。对于节点值对,必须对上面的表进行操作,如下所示:

+----------+----------------+-----------------+
| Customer | Origin Website | Landing Website |
+----------+----------------+-----------------+
| john     | facebook       | microsoft       |
+----------+----------------+-----------------+
| john     | microsoft      | facebook        |
+----------+----------------+-----------------+
| steven   | apple          | stackoverflow   |
+----------+----------------+-----------------+
| steven   | stackoverflow  | cnn             |
+----------+----------------+-----------------+  

这是否可以仅使用oracle SQL?

2 个答案:

答案 0 :(得分:3)

您可以使用LEAD()执行此操作:

Select  Customer, 
        Website As "Origin Website",
        Lead(Website) Over (Partition By Customer Order By Click) As "Landing Website"
From    YourTable

答案 1 :(得分:2)

像这样的东西。我添加了一个专栏来展示" hops"从一个网站到另一个网站;否则你会知道从哪里到哪里,但不知道是以什么顺序。

with
     test_data ( Customer, Click, Website ) as (
       select 'john'  , 1, 'facebook'      from dual union all
       select 'john'  , 2, 'microsoft'     from dual union all
       select 'john'  , 3, 'facebook'      from dual union all
       select 'steven', 1, 'apple'         from dual union all
       select 'steven', 2, 'stackoverflow' from dual union all
       select 'steven', 3, 'cnn'           from dual
     )
-- end of test data; the SQL query begins below this line
select *
from (
       select customer, 
              row_number() over (partition by customer order by click) as ord, 
              website as origin_website,
              lead(website) over (partition by customer order by click) as landing_website
       from   test_data
     )
where landing_website is not null
order by customer, ord
;

CUSTOMER      ORD ORIGIN_WEBSITE LANDING_WEBSITE
--------- ------- -------------- ---------------
john            1 facebook       microsoft
john            2 microsoft      facebook
steven          1 apple          stackoverflow
steven          2 stackoverflow  cnn

4 rows selected.