views:

86

answers:

1

Hi!

I have one huge spreadsheet file (1000+ lines) and one postgreSQL table. I need to compare spreadsheet data with postgresql table data and add fill the blank fields in table with data from spreadsheet and add entries not present in db table.

Yes, I can convert (via csv) whole spreadsheet into database table. But, there are unique values in both documents, so I will lose data doing this. Or, is it possible to compare 2 tables and fill missing fields in table A with data from table B?

Thanks in advance!

+1  A: 

It's easy in SQL to compare two tables, and insert rows not in one table. For example:

INSERT INTO TableA
    (col1, col2, col3)
SELECT
    col1, col2, col3
FROM SpreadSheetTable
WHERE NOT EXISTS (
    SELECT *
    FROM TableA
    WHERE TableA.col1 = SpreadSheetTable.col1
)

This query inserts all rows from SpreadSheetTable into TableA, except those rows for which TableA already contains a row with the same "col1" value.

Andomar