MySQL:匹配列的年度

时间:2018-04-16 02:40:19

标签: mysql join

这是一个有助于澄清我的问题的转贴。我正在处理时间序列数据,我需要参考前一年的数据。在excel中,我只使用vlookup或索引匹配。

这是我的表

+------------+--------------+--------------+------+-------+
| date       | unique_id    | prev_year_id | id   | value |
+------------+--------------+--------------+------+-------+
| 2016-01-01 | 1-2016-01-01 | 1-2015-01-01 |    1 |     7 |
| 2016-01-01 | 2-2016-01-01 | 2-2015-01-01 |    2 |    19 |
| 2016-01-01 | 3-2016-01-01 | 3-2015-01-01 |    3 |     6 |
| 2016-01-01 | 4-2016-01-01 | 4-2015-01-01 |    4 |    13 |
| 2016-01-01 | 5-2016-01-01 | 5-2015-01-01 |    5 |     5 |
| 2017-01-01 | 1-2017-01-01 | 1-2016-01-01 |    1 |    17 |
| 2017-01-01 | 2-2017-01-01 | 2-2016-01-01 |    2 |     8 |
| 2017-01-01 | 3-2017-01-01 | 3-2016-01-01 |    3 |    20 |
| 2017-01-01 | 4-2017-01-01 | 4-2016-01-01 |    4 |     3 |
| 2017-01-01 | 5-2017-01-01 | 5-2016-01-01 |    5 |     0 |
| 2018-01-01 | 1-2018-01-01 | 1-2017-01-01 |    1 |     4 |
| 2018-01-01 | 2-2018-01-01 | 2-2017-01-01 |    2 |    21 |
| 2018-01-01 | 3-2018-01-01 | 3-2017-01-01 |    3 |     7 |
| 2018-01-01 | 4-2018-01-01 | 4-2017-01-01 |    4 |     3 |
| 2018-01-01 | 5-2018-01-01 | 5-2017-01-01 |    5 |     6 |
+------------+--------------+--------------+------+-------+

我的专栏是:

日期下, unique_id (这是id和date的连接在一起;主键), prev_year_id (id和日期的连续 - 间隔1年), 的 ID 下, 的

我需要帮助创建一个语句以匹配prev_year_id和unique_id并返回行的值。

由于没有任何数据可供参考,第一年会有差距,但在第一年之后,每个prev_year_value将与之前一年的unique_id匹配

这就是我想象的最终结果:

+------------+--------------+--------------+------+-------+-----------------+
| date       | unique_id    | prev_year_id | id   | value | prev_year_value |
+------------+--------------+--------------+------+-------+-----------------+
| 2016-01-01 | 1-2016-01-01 | 1-2015-01-01 |    1 |     7 | null            |
| 2016-01-01 | 2-2016-01-01 | 2-2015-01-01 |    2 |    19 | null            |
| 2016-01-01 | 3-2016-01-01 | 3-2015-01-01 |    3 |     6 | null            |
| 2016-01-01 | 4-2016-01-01 | 4-2015-01-01 |    4 |    13 | null            |
| 2016-01-01 | 5-2016-01-01 | 5-2015-01-01 |    5 |     5 | null            |
| 2016-01-01 | 1-2017-01-01 | 1-2016-01-01 |    1 |    17 | 7               |
| 2016-01-01 | 2-2017-01-01 | 2-2016-01-01 |    2 |     8 | 19              |
| 2016-01-01 | 3-2017-01-01 | 3-2016-01-01 |    3 |    20 | 6               |
| 2016-01-01 | 4-2017-01-01 | 4-2016-01-01 |    4 |     3 | 13              |
| 2016-01-01 | 5-2017-01-01 | 5-2016-01-01 |    5 |     0 | 5               |
| 2016-01-01 | 1-2018-01-01 | 1-2017-01-01 |    1 |     4 | 17              |
| 2016-01-01 | 2-2018-01-01 | 2-2017-01-01 |    2 |    21 | 8               |
| 2016-01-01 | 3-2018-01-01 | 3-2017-01-01 |    3 |     7 | 20              |
| 2016-01-01 | 4-2018-01-01 | 4-2017-01-01 |    4 |     3 | 3               |
| 2016-01-01 | 5-2018-01-01 | 5-2017-01-01 |    5 |     6 | 0               |
+------------+--------------+--------------+------+-------+-----------------+

这是我的陈述..

SELECT A.*, B.value as 'prev_year_value'
FROM table1 A
INNER JOIN table1 B ON A.current_month_code = B.`prev_year_code

结果返回非常慢,它返回的是下一年的价值,而不是前一年的价值。

1 个答案:

答案 0 :(得分:0)

ActiveWorkbook.Queries.Add Name:="NameOfTheFile", Formula:= "let" & Chr(13) & "" & Chr(10) & _
    "    Quelle = Csv.Document(File.Contents(""C:\Users\USERNAME\Desktop\censored\NameOfTheFile.csv""),[Delimiter="";"", Columns=18, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & _
    "    #""Geänderter Typ"" = Table.TransformColumnTypes(Quelle,{{""Column1"", type text},{""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text},{""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text},{""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text},{""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text},{""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", type text},{""Column18"", type text}})" & Chr(13) & "" & Chr(10) & _
    "in" & Chr(13) & "" & Chr(10) & "    #""Geänderter Typ"""

由于这是左连接,如果“ON”语句没有结果,结果仍将包含SELECT a.*, b.`value` as `prev_year_value` FROM `table1` a LEFT JOIN `table1` b ON a.`prev_year_id` = b.`unique_id` 的值,而a最终将为null,如你想要的。

在你尝试过的内连接中,它只包含结果,两个表中都有匹配