Reorganising Data in SQLite

前端 未结 1 359
别那么骄傲
别那么骄傲 2021-01-26 02:39

AS a beginner on Python and SQLite I am wandering if there are more elegant solutions for the tasks I have.

My current SQLite database contains several time series as:

相关标签:
1条回答
  • 2021-01-26 02:58

    The timestamps can simply be copied:

    INSERT INTO TimeStamps(time)
    SELECT time FROM Y1 UNION
    SELECT time FROM Y2 UNION
    SELECT time FROM Y3 UNION
    SELECT time FROM Y4 UNION
    SELECT time FROM Y5 UNION
    SELECT time FROM Y6;
    

    The datapoints require a lookup (using a correlated subquery) for the corresponding timestamp ID:

    INSERT INTO DataPoints(timeID, VarID, value)
    SELECT (SELECT timeID
            FROM TimeStamps
            WHERE time = all_Y.time),
           VarID,
           value
    FROM (SELECT time, 1 AS VarID, value FROM Y1 UNION ALL
          SELECT time, 2,          value FROM Y2 UNION ALL
          SELECT time, 3,          value FROM Y3 UNION ALL
          SELECT time, 4,          value FROM Y4 UNION ALL
          SELECT time, 5,          value FROM Y5 UNION ALL
          SELECT time, 6,          value FROM Y6) AS all_Y;
    
    0 讨论(0)
提交回复
热议问题