Matching data from two tables with unequal number of rows

vanellope1

I have 2 tables, and I am trying to match up the data but all the answers have suggested right joins or full joins, which are not available on SQLite.

Table 1: 

╔═════════╦═════════╦══════╗
║ Company ║ Product ║ Cost ║
╠═════════╬═════════╬══════╣
║ A       ║ abc     ║  100 ║ 
║ B       ║ abc     ║  150 ║
║ F       ║ xyz     ║  250 ║
║ G       ║ xyz     ║  300 ║
╚═════════╩═════════╩══════╝

However I have a list of more companies (with same products)

Table 2:

╔═════════╦═════════╗
║ Product ║ Company ║
╠═════════╬═════════╣
║ abc     ║ A       ║
║ abc     ║ B       ║
║ abc     ║ C       ║
║ abc     ║ D       ║
║ abc     ║ E       ║
║ abc     ║ F       ║
║ abc     ║ G       ║
║ xyz     ║ A       ║
║ xyz     ║ B       ║
║ xyz     ║ C       ║
║ xyz     ║ D       ║
║ xyz     ║ E       ║
║ xyz     ║ F       ║
║ xyz     ║ G       ║
╚═════════╩═════════╝

How do I match them up so they look like this?

Table 3:

╔═════════╦═════════╦══════╗
║ Product ║ Company ║ Cost ║
╠═════════╬═════════╬══════╣
║ abc     ║ A       ║ 100  ║
║ abc     ║ B       ║ 150  ║
║ abc     ║ C       ║ null ║
║ abc     ║ D       ║ null ║
║ abc     ║ E       ║ null ║
║ abc     ║ F       ║ null ║
║ abc     ║ G       ║ null ║
║ xyz     ║ A       ║ null ║
║ xyz     ║ B       ║ null ║
║ xyz     ║ C       ║ null ║
║ xyz     ║ D       ║ null ║
║ xyz     ║ E       ║ null ║
║ xyz     ║ F       ║ 250  ║
║ xyz     ║ G       ║ 300  ║
╚═════════╩═════════╩══════╝

When I use this code,

SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.product = f.product
WHERE t.company = f.company

it only returns [Company] with an associated [Product] and [Cost], but does not return [Company] with null values.

When I use

SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.company = f.company

then my output looks like

╔═══════════╦═══════════╦═════════╗
║ t.Company ║ f.Company ║ Product ║
╠═══════════╬═══════════╬═════════╣
║ A         ║ A         ║ abc     ║
║ B         ║ A         ║ abc     ║
║ F         ║ A         ║ abc     ║
║ G         ║ A         ║ abc     ║
║ A         ║ B         ║ abc     ║
║ B         ║ B         ║ abc     ║
║ F         ║ B         ║ abc     ║
║ G         ║ B         ║ abc     ║
║ A         ║ C         ║ abc     ║
║ B         ║ C         ║ abc     ║
║ F         ║ C         ║ abc     ║
║ G         ║ C         ║ abc     ║
╚═══════════╩═══════════╩═════════╝

Any help will be much appreciated. Thank you!

Paul Roub

SQLite does support LEFT OUTER JOIN, which should do the job just fine:

select two.product, two.company, one.cost from two 
 left outer join one on 
   ((one.company = two.company) and (one.product = two.product));

(where two is your "table 2" and one is your "table 1")

Running this in SQLite with the above data:

abc|A|100
abc|B|150
abc|C|
abc|D|
abc|E|
abc|F|
abc|G|
xyz|A|
xyz|B|
xyz|C|
xyz|D|
xyz|E|
xyz|F|250
xyz|G|300

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find total number of rows using data from 3 tables, MySQL

From Dev

Get the total number of rows from two different tables with the same id drops me more rows

From Dev

Sum data from two tables with different number of rows

From Dev

Merge rows with equal and unequal data

From Dev

How to create a random matching between the rows of two data.tables (or data.frames)

From Dev

perl matching two columns from different tables

From Dev

Get matching rows between two tables and NULL otherwise on left table

From Dev

efficient way substracting two very large data frames with different number of rows (matching XYZ)

From Dev

compare two columns from different tables and append values for matching rows to first file

From Dev

Oracle SQL to compare data and retrieve un-matching rows between two tables in Oracle DB

From Dev

Add (not merge!) two data frames with unequal rows and columns

From Dev

select matching data from mysql tables

From Dev

Matching two list of unequal length

From Dev

Joining 3 tables with unequal number of records

From Dev

How to concatenate data frames with unequal number of rows and different column names

From Dev

Join data from two tables

From Dev

Getting data from two tables

From Dev

cbind in for loop with unequal number of rows

From Dev

Selecting both matching Rows from 2 tables

From Dev

Count number of matching data in columns on the basis of primary key in two tables

From Dev

how to count the number of rows fetched from the query consists of data from many tables

From Dev

Sum data from two tables with different number of rows

From Dev

perl matching two columns from different tables

From Dev

Make a SELECT query from two tables with one record from matching rows in mysql

From Dev

Computing number of bits that are set to 1 for matching rows in terms of hamming distance between two data frames

From Dev

select matching data from mysql tables

From Dev

Matching two list of unequal length

From Dev

Selecting and matching data from 3 tables

From Dev

Matching two data tables (Vlookup, dplyr, match(), left_join) keeping number of rows

Related Related

  1. 1

    Find total number of rows using data from 3 tables, MySQL

  2. 2

    Get the total number of rows from two different tables with the same id drops me more rows

  3. 3

    Sum data from two tables with different number of rows

  4. 4

    Merge rows with equal and unequal data

  5. 5

    How to create a random matching between the rows of two data.tables (or data.frames)

  6. 6

    perl matching two columns from different tables

  7. 7

    Get matching rows between two tables and NULL otherwise on left table

  8. 8

    efficient way substracting two very large data frames with different number of rows (matching XYZ)

  9. 9

    compare two columns from different tables and append values for matching rows to first file

  10. 10

    Oracle SQL to compare data and retrieve un-matching rows between two tables in Oracle DB

  11. 11

    Add (not merge!) two data frames with unequal rows and columns

  12. 12

    select matching data from mysql tables

  13. 13

    Matching two list of unequal length

  14. 14

    Joining 3 tables with unequal number of records

  15. 15

    How to concatenate data frames with unequal number of rows and different column names

  16. 16

    Join data from two tables

  17. 17

    Getting data from two tables

  18. 18

    cbind in for loop with unequal number of rows

  19. 19

    Selecting both matching Rows from 2 tables

  20. 20

    Count number of matching data in columns on the basis of primary key in two tables

  21. 21

    how to count the number of rows fetched from the query consists of data from many tables

  22. 22

    Sum data from two tables with different number of rows

  23. 23

    perl matching two columns from different tables

  24. 24

    Make a SELECT query from two tables with one record from matching rows in mysql

  25. 25

    Computing number of bits that are set to 1 for matching rows in terms of hamming distance between two data frames

  26. 26

    select matching data from mysql tables

  27. 27

    Matching two list of unequal length

  28. 28

    Selecting and matching data from 3 tables

  29. 29

    Matching two data tables (Vlookup, dplyr, match(), left_join) keeping number of rows

HotTag

Archive