Quantcast
Channel: SQL conditional insertion of data. SQLite - Stack Overflow
Viewing all articles
Browse latest Browse all 3

SQL conditional insertion of data. SQLite

$
0
0

This is essentially a question about inserting data into one table dependent on information in another table. The db is implemented with sqlite3. I am a relatively new user of SQL.

I have two tables, CUSTOMERS and MISSING as shown below (spaces indicating NULL).

CUSTOMERS:ID|V1|V2|V31|18|23|142| 7|  |114|  |14|7|19|22|12MISSING:ID|V1|V2|V3 2| N| Y| N 3| Y| Y| Y 4| Y| N| Y 5| Y| Y| Y 8| Y| Y| Y

Table MISSING indicates which customer IDs in table CUSTOMERS do not have values recorded for certain variables. So, for example, customer 2 is missing a value for variable V2 but not for V1 or V3. Similarly, customers 3, 5 and 8 are missing values for all three variables V1, V2, V3. Note that customer 6 does not exist, so there is no record in either CUSTOMERS or MISSING.

When a customer has incomplete missing data (i.e., where, as with customer 4, the value of only 2 or the three variables is missing) there is a already a record in table CUSTOMERS, and I don't want to make any change. But when a customer is missing values on all three variables (as with customers 3, 5 and 8), I'd like to add a record to CUSTOMERS with NULLs in V1, V2 and V3, so that the table appears as follows.

CUSTOMERS:ID|V1|V2|V3 1|18|23|14 2| 7|  |11 3|  |  |   4|  |14| 5|  |  |   6|  |  |   7|19|22|12 8|  |  |  

I know how to select the IDs from MISSING that show that there is missing information for V1, V2 and V3 ... but how do I use that to make the desired changes to CUSTOMERS?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images