문제

I would like to self join a table with its "future self". Or append a column with the value one week later (or any other periodicity). Let's say that I have a table with a date, two identifiers, and a value. In the query I would like to get the value, as well as the value for the same set of indentifiers one week later.

For a newbie this is two questions. First, when I have several identifiers (in the real problem I have six identifiers that describe a unique entry for each date) should I create my own identifier for that item? Second, how do I do this one week lag, particularly across a change in months?

Here's an example data set:

       date id_1 id_2 value value_future
1  20101224    a    c     1           NA
2  20101224    a    d     2           NA
3  20101224    b    c     3           NA
4  20101224    b    d     4           NA
5  20101225    a    c     5           NA
6  20101225    a    d     6           NA
7  20101225    b    c     7           NA
8  20101225    b    d     8           NA
9  20101226    a    c     9           NA
10 20101226    a    d    10           NA
11 20101226    b    c    11           NA
12 20101226    b    d    12           NA
13 20101227    a    c    13           NA
14 20101227    a    d    14           NA
15 20101227    b    c    15           NA
16 20101227    b    d    16           NA
17 20101228    a    c    17           NA
18 20101228    a    d    18           NA
19 20101228    b    c    19           NA
20 20101228    b    d    20           NA
21 20101229    a    c    21           NA
22 20101229    a    d    22           NA
23 20101229    b    c    23           NA
24 20101229    b    d    24           NA
25 20101230    a    c    25           NA
26 20101230    a    d    26           NA
27 20101230    b    c    27           NA
28 20101230    b    d    28           NA
29 20101231    a    c    29            1
30 20101231    a    d    30            2
31 20101231    b    c    31            3
32 20101231    b    d    32            4
33 20110101    a    c    33            5
34 20110101    a    d    34            6
35 20110101    b    c    35            7
36 20110101    b    d    36            8
37 20110102    a    c    37            9
38 20110102    a    d    38           10
39 20110102    b    c    39           11
40 20110102    b    d    40           12

Thanks!

도움이 되었습니까?

해결책

I don't know SQLLite very well, so I had to look up how to do date additions, but I think this might work.

SELECT  c.date, c.id_1, c.id_2, c.value, f.value as value_future
FROM mytable c
LEFT OUTER JOIN mytable f
ON date(c.date, '+7 day') = f.date AND c.id_1 = f.id_1 AND c.id_2 = f.id_2

Basically you want to do a join to the same table which requires you to alias both tables in the join (c and f). Use the date function in the ON clause to join to the record a week from now, you can adjust the date modifier clause (+7 day) as necessary to change the date range. Then just tack on all the identifiers that you have. In general it is a good idea to create a single primary key but in this case it wouldn't help you because there would be no way to figure out the key of the record you would want to join for it. Last thing to point out is that you now have 2 "value" columns in the result so you need to alias one of them to make the column list unique. That is what the "as value_future" does.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top