Multiple select for aleady joined table

being_uncertain

Below is a part of my select query. In the same query I am selecting COLUMN_1 from a table TABLE2 with condition check. Also I am joining this table at end with one of the condition in the inner select as below. Can we have any other way to handle this situation with out using multiple `SELECT inside.

SELECT 
    T1.COLUMN_1
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='A' AND COLUMN_11=T2.COLUMN_11)
    , T1.COLUMN_2
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='B' AND COLUMN_11=T2.COLUMN_11)
    , T1.COLUMN_3
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='C' AND COLUMN_11=T2.COLUMN_11)
    , T1.COLUMN_4
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='D' AND COLUMN_11=T2.COLUMN_11)
    , T1.COLUMN_5
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='E' AND COLUMN_11=T2.COLUMN_11)
    , T1.COLUMN_6
    , (SELECT COLUMN_1 FROM TABLE2 WHERE COLUMN_22 ='F' AND COLUMN_11=T2.COLUMN_11)
FROM TABLE1 T1, TABLE2 T2  
    -- plus two more tables
    --plus some other conditions
WHERE T1.COLUMN_11=T2.COLUMN_11
sstan

Use CASE instead:

SELECT T1.COLUMN_1
    ,CASE 
        WHEN T2.COLUMN_22 = 'A'
            THEN T2.COLUMN_1
        END
    ,T1.COLUMN_2
    ,CASE 
        WHEN T2.COLUMN_22 = 'B'
            THEN T2.COLUMN_1
        END
    ,T1.COLUMN_3
    ,CASE 
        WHEN T2.COLUMN_22 = 'C'
            THEN T2.COLUMN_1
        END
    ,T1.COLUMN_4
    ,CASE 
        WHEN T2.COLUMN_22 = 'D'
            THEN T2.COLUMN_1
        END
    ,T1.COLUMN_5
    ,CASE 
        WHEN T2.COLUMN_22 = 'E'
            THEN T2.COLUMN_1
        END
    ,T1.COLUMN_6
    ,CASE 
        WHEN T2.COLUMN_22 = 'F'
            THEN T2.COLUMN_1
        END
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.COLUMN_11 = T2.COLUMN_11;

EDIT

I changed the query to use the ansi join syntax. But that change is irrelevant to what you are asking. You can keep your join syntax if you want. The only relevant change is in the SELECT portion of the query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use LISTAGG to select multiple rows on joined table

From Java

SQL How to select from multiple values in joined table

From Dev

LINQ does not recognize joined table in select clause

From Dev

Select from joined table only if record exists

From Dev

laravel 5.1 eloquent select with prefix joined table

From Dev

select record from joined table if it exists

From Dev

Laravel mysql select count of joined table

From Dev

Select latest from joined table excluding duplicates

From Dev

MySQL select SUM when a table is joined

From Dev

How to select last record by date in a joined table

From Dev

Select only duplicates from inner joined table

From Dev

Unable to select data from another joined table

From Dev

How to combine multiple records from a joined table

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

MySQL select row with two matching joined rows from another table

From Dev

Select columns from 2 joined table doctrine/symfony2

From Dev

How to select a minimal value from a joined table with JPA?

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

SQL select flag based on count and/or flag of joined table

From Dev

Join sql tables to select records which does not exists in joined table

From Dev

Filtering MySQL Select based on joined table's fields

From Dev

MySql select joined rows that don't exists in third table

From Dev

SQL Server : splitting a column to multiple columns with joined table

From Dev

Joining multiple tables in PostgreSQL, and counting joined table rows

From Dev

How to improve performance of multiple joined table in sql query

From Dev

How to use GroupBy correctly from multiple joined table

From Dev

MySQL multiple table join Update and Delete where joined data unknown

From Dev

SQL - Get average for multiple, but not all, rows of joined table

From Dev

SELECT query on multiple table

Related Related

  1. 1

    Use LISTAGG to select multiple rows on joined table

  2. 2

    SQL How to select from multiple values in joined table

  3. 3

    LINQ does not recognize joined table in select clause

  4. 4

    Select from joined table only if record exists

  5. 5

    laravel 5.1 eloquent select with prefix joined table

  6. 6

    select record from joined table if it exists

  7. 7

    Laravel mysql select count of joined table

  8. 8

    Select latest from joined table excluding duplicates

  9. 9

    MySQL select SUM when a table is joined

  10. 10

    How to select last record by date in a joined table

  11. 11

    Select only duplicates from inner joined table

  12. 12

    Unable to select data from another joined table

  13. 13

    How to combine multiple records from a joined table

  14. 14

    MySQL: Select records where joined table matches ALL values

  15. 15

    MySQL select row with two matching joined rows from another table

  16. 16

    Select columns from 2 joined table doctrine/symfony2

  17. 17

    How to select a minimal value from a joined table with JPA?

  18. 18

    MySQL: Select records where joined table matches ALL values

  19. 19

    SQL select flag based on count and/or flag of joined table

  20. 20

    Join sql tables to select records which does not exists in joined table

  21. 21

    Filtering MySQL Select based on joined table's fields

  22. 22

    MySql select joined rows that don't exists in third table

  23. 23

    SQL Server : splitting a column to multiple columns with joined table

  24. 24

    Joining multiple tables in PostgreSQL, and counting joined table rows

  25. 25

    How to improve performance of multiple joined table in sql query

  26. 26

    How to use GroupBy correctly from multiple joined table

  27. 27

    MySQL multiple table join Update and Delete where joined data unknown

  28. 28

    SQL - Get average for multiple, but not all, rows of joined table

  29. 29

    SELECT query on multiple table

HotTag

Archive