SQL SELECT from TABLE B based on column but grab all rows based on TABLE A ID

expenguin

So I've been hit with a dozy this morning.

I've got two tables listed below.

TABLE A
InvoiceNo | Total
000001    | $100
000002    | $50
000003    | $200
etc..

TABLE B
InvoiceNo | ItemCode
000001    | Item-A
000001    | Item-B
000001    | Item-C
000002    | Item-A
000003    | Item-B
000003    | Item-D
etc...

I need to select out of table 2 where ITEM-B exists, but also grab all other items based on the InvoiceNo. So it should grab ItemCode > InvoiceNo > All Items based on that InvoiceNo

OUTPUT
InvoiceNo | Total | ItemCode
000001    | $100  | Item-A
000001    | $100  | Item-B
000001    | $100  | Item-C
000003    | $200  | Item-B
000003    | $200  | Item-D

The query I'm currently working with is:

SELECT a.InvoiceNo, a.Total, b.ItemCode FROM TableA a
LEFT JOIN TABLE B b ON a.InvoiceNo = b.InvoiceNo
WHERE b.ItemCode = 'Item-B'

This generates the following output

INCORRECT OUTPUT
InvoiceNo | Total | ItemCode
000001    | $100  | Item-B
000003    | $200  | Item-B

I'm not quite sure how to approach this, any clues or assistance would be very appreciated.

Cheers

John Woo

There are many ways to do this. One way is by using EXISTS

SELECT  a.InvoiceNo, A.Total, B.ItemCode
FROM    TableA a
        INNER JOIN TableB b
            ON a.InvoiceNo = b.InvoiceNo
WHERE   EXISTS (SELECT 1
                FROM TableB c
                WHERE b.InvoiceNo = c.InvoiceNo
                        AND c.ItemCode = 'Item-B')

Here's a Demo.

Another way is by using INNER JOIN on a subquery which only get all invoices having ItemB

SELECT  a.InvoiceNo, A.Total, B.ItemCode
FROM    TableA a
        INNER JOIN TableB b
            ON a.InvoiceNo = b.InvoiceNo
        INNER JOIN (SELECT InvoiceNo FROM TableB c WHERE ItemCode = 'Item-B') c
            ON a.InvoiceNo = c.InvoiceNo

Here's a Demo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select multiple rows from table based on ID

From Dev

Excel Pivot Table select rows based on column, with all columns displayed

From Dev

SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

From Dev

Select all rows in table1 and all matched rows in table2 in same row based on ID

From Dev

SQL: Find all records in Table A that do not have a value in Table B based on a column value in Table B

From Dev

SQL SELECT Data from other table based on column value

From Dev

Select table and column dynamically based on other table rows

From Dev

SQL PIVOT a table based on a column with ID's

From Dev

select rows from table based on data from another table

From Dev

select rows from table based on data from another table

From Dev

SQL Server: Insert Multiple Rows to a table based on a column in a different table

From Dev

MYSQL: Select from table A, based on seen date of user in Table B

From Dev

Select rows from a table based on max value in different table

From Dev

Select rows from a table based on max value in different table

From Java

SQL query to select from one table based on a column value in other table

From Dev

SQL Insert in to TAble B based using data from Table A

From Dev

Select ALL Columns | Rows of a data.table based on condition

From Dev

SQL Select rows where all rows from linked table have the same value in column x

From Dev

Oracle trigger to update all the rows of table based on the data of updating column

From Java

Is there a more idiomatic way to select rows from a PyArrow table based on contents of a column?

From Dev

MySQL CodeIgniter Select or not from second table based on column of first Select

From Dev

SQL: Linking Multiple Rows in Table Based on Data Chain in Select

From Dev

How to select all from table one based on a value, then select from same table with value from that table?

From Dev

SQL table : update column with batchName based on the total count of rows

From Dev

sql-left-outer-join-with rows-based on column value from right table

From Dev

Laravel select random rows from table based on another field

From Dev

INSERT rows multiple times based on a column value from another table

From Dev

SQL Select - Return same column twice based on IDs in another table

From Dev

SQL Select - Return same column twice based on IDs in another table

Related Related

  1. 1

    Select multiple rows from table based on ID

  2. 2

    Excel Pivot Table select rows based on column, with all columns displayed

  3. 3

    SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

  4. 4

    Select all rows in table1 and all matched rows in table2 in same row based on ID

  5. 5

    SQL: Find all records in Table A that do not have a value in Table B based on a column value in Table B

  6. 6

    SQL SELECT Data from other table based on column value

  7. 7

    Select table and column dynamically based on other table rows

  8. 8

    SQL PIVOT a table based on a column with ID's

  9. 9

    select rows from table based on data from another table

  10. 10

    select rows from table based on data from another table

  11. 11

    SQL Server: Insert Multiple Rows to a table based on a column in a different table

  12. 12

    MYSQL: Select from table A, based on seen date of user in Table B

  13. 13

    Select rows from a table based on max value in different table

  14. 14

    Select rows from a table based on max value in different table

  15. 15

    SQL query to select from one table based on a column value in other table

  16. 16

    SQL Insert in to TAble B based using data from Table A

  17. 17

    Select ALL Columns | Rows of a data.table based on condition

  18. 18

    SQL Select rows where all rows from linked table have the same value in column x

  19. 19

    Oracle trigger to update all the rows of table based on the data of updating column

  20. 20

    Is there a more idiomatic way to select rows from a PyArrow table based on contents of a column?

  21. 21

    MySQL CodeIgniter Select or not from second table based on column of first Select

  22. 22

    SQL: Linking Multiple Rows in Table Based on Data Chain in Select

  23. 23

    How to select all from table one based on a value, then select from same table with value from that table?

  24. 24

    SQL table : update column with batchName based on the total count of rows

  25. 25

    sql-left-outer-join-with rows-based on column value from right table

  26. 26

    Laravel select random rows from table based on another field

  27. 27

    INSERT rows multiple times based on a column value from another table

  28. 28

    SQL Select - Return same column twice based on IDs in another table

  29. 29

    SQL Select - Return same column twice based on IDs in another table

HotTag

Archive