SQL - INSERT INTO multiple columns from multiple selections

PyThagoras

I have a MariaDB database.

Inside that DB, I have the following table, table1:

|     id       |     timestamp     |  unit |  detector  |    value    |
 -------------------------------------------------------------------------
     UUID()         2020-12-02          1        1             0.1
     UUID2()        2020-12-02          1        2             0.2
     UUID3()        2020-12-02          2        1             0.3
     UUID4()        2020-12-02          2        2             0.4
     UUID5()        2020-12-03          1        1             0.5
     UUID6()        2020-12-03          1        2             0.6
     UUID7()        2020-12-03          2        1             0.7
     UUID8()        2020-12-03          2        2             0.8

I have been asked to map the data to this new table, table2

 |     id       |     timestamp     |  detector 11   |   detector 12   |  detector 21  |  detector 22 |
  ----------------------------------------------------------------------------------------------------
       UUI9()         2020-12-02            0.1              0.2             0.3             0.4
       UUID10()        2020-12-03            0.5              0.6             0.7             0.8

The only difference from this situation is that I have 100 detector and unit combinations and 36 million rows. I have written code that can get the desired value for 1 detector but I can't figure out a way of doing multiple rows -> columns at the same time. No way I'm doing this manually, it would take weeks.

INSERT INTO table2
(id, timestamp, detector11)
SELECT UUID(), t1.timestamp, t1.value FROM table1 t1
WHERE t1.unit='1' AND t1.detector='1'
ORDER BY timestamp ;

This successfully translates the data from table1 where (detector=1, unit=1) to the column (detector11) with a good timestamp. However, now I have all the other columns except for id, timestamp, and detector11 to NULL.

Ideally, someone could help me to code something like that:

INSERT INTO table2
(id, timestamp, detector11, detector12, detector21, detector22)
SELECT UUID(), t1.timestamp, 
VALUES(t1.value FROM table1 t1
       WHERE t1.unit='1' AND t1.detector='1'
       ORDER BY timestamp,

       t1.value FROM table1 t1
       WHERE t1.unit='1' AND t1.detector='2'
       ORDER BY timestamp,

       t1.value FROM table1 t1
       WHERE t1.unit='2' AND t1.detector='1'
       ORDER BY timestamp,

       t1.value FROM table1 t1
       WHERE t1.unit='2' AND t1.detector='2'
       ORDER BY timestamp) ;

Which would fill all the columns at the same time.

nbk

You can only join the separate Tables.

The problem is to join the values, there you have to see if your ON statement for the join is so valid

CREATE TABLE table2 (id varchar(20), `timestamp` TIMESTAMP, detector11 DECIMAL(4,2), detector12 DECIMAL(4,2)
, detector21 DECIMAL(4,2), detector22 DECIMAL(4,2))
CREATE TABLE table1 (`timestamp`TIMESTAMP, value DECIMAL(4,2), unit INT,detector INT)
INSERT INTO table2
(id, timestamp, detector11, detector12, detector21, detector22)
SELECT UUID(),t1a.timestamp, t1a.detector11,t1b.detector12,t1c.detector21,t1d.detector22
FROM
(SELECT
t1.timestamp, t1.value as detector11 FROM table1 t1
WHERE t1.unit='1' AND t1.detector='1'
ORDER BY timestamp ) t1a
JOIN
  (SELECT t1.timestamp, t1.value AS detector12 FROM table1 t1
       WHERE t1.unit='1' AND t1.detector='2'
       ORDER BY timestamp) t1b
       ON t1a.timestamp = t1b.timestamp
JOIN       
  (SELECT t1.timestamp,t1.value AS detector21 FROM table1 t1
       WHERE t1.unit='2' AND t1.detector='1'
       ORDER BY timestamp) t1c
       ON t1a.timestamp = t1c.timestamp
JOIN
  (SELECT t1.timestamp,t1.value AS detector22 FROM table1 t1
       WHERE t1.unit='2' AND t1.detector='2'
       ORDER BY timestamp) t1d
  ON t1a.timestamp = t1d.timestamp

db<>fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Query, Multiple Selections

From Dev

SQL INSERT INTO from multiple tables

From Dev

SQL INSERT INTO from multiple tables

From Dev

SQL: Insert multiple row with common columns

From Dev

SQL: Insert multiple row with common columns

From Dev

SQL average from multiple columns

From Dev

Update multiple rows in sql table for checkbox selections

From Dev

Update multiple rows in sql table for checkbox selections

From Dev

INSERT multiple columns

From Dev

Multiple selections from a list using Python

From Dev

Get values from multiple selections with querySelectorAll

From Dev

SQL Multiple Row Insert w/ multiple selects from different tables

From Dev

SQL IN operator for multiple columns from different tables

From Dev

Select multiple columns from a subquery in SQL Server

From Dev

SQL Pivot from multiple tables and columns

From Dev

Update a column from multiple columns in SQL Server

From Dev

SQL Derive multiple columns from one column

From Dev

insert into select not exists sql server multiple columns primary key voilation

From Dev

SQL INSERT Statement By Comparing and Retrieving Data Involving Multiple Columns

From Dev

Handsontable dropdowns with multiple selections

From Dev

Display on multiple selections

From Dev

PostgreSQL: Conditional INSERT INTO on some of multiple columns from difference tables

From Dev

Multiple insert SQL oracle

From Dev

SQL: Insert Into with multiple rows

From Dev

INSERT with Multiple select SQL

From Dev

INSERT with Multiple select SQL

From Dev

For Loop insert multiple SQL

From Dev

Preparing insert statement for multiple columns

From Dev

INSERT INTO ... RETURNING multiple columns (PostgreSQL)

Related Related

HotTag

Archive