MySQL Return the first non-NULL value from the list of the column in select statement with column name

user4676307

I have a sql query below:

SELECT 
    md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther
FROM
    marketing_details md
WHERE
    md.id = 14588

From the 7 columns in the above select statement only one column will have a value and rest will be null. Is it possible to select just that one column value that is not null using some sort of sql statement ?

Shadow

Use the coalesce() function to return the 1st non-null value from a list of parameters:

SELECT 
    coalesce(md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther) as non_null_value
FROM
    marketing_details md
WHERE
    md.id = 14588

However, it will not be able to tell you which column the value came from.

UPDATE

If you really want to use sql to retrieve the name of the field that has the non null value, then you can do that with the following monstrous sql statement below. What it does it concatenates each field value from a record into a single string, where the values are separated by comma. NULL values are converted to empty string. Then using find_in_set() function it finds the position of the only non null value within the above string. Then using the elt() function it returns the name of the field from the list of field name literals based on the position returned by find_in_set().

SELECT
    md.id, 
    coalesce(md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther) as non_null_value,
    elt(find_in_set(coalesce(md.refereeInternetSearch,
                                 md.refereeCompanyColleague,
                                 md.refereeIndustryPeer,
                                 md.refereeIndustryEvent,
                                 md.refereeIndustryPublication,
                                 md.refereeMarketingEmail,
                                 md.refereeOther),
                        concat(coalesce(md.refereeInternetSearch,''),',',
                               coalesce(md.refereeCompanyColleague,''),',',
                               coalesce(md.refereeIndustryPeer,''),',',
                               coalesce(md.refereeIndustryEvent,''),',',
                               coalesce(md.refereeIndustryPublication,''),',',
                               coalesce(md.refereeMarketingEmail,''),',',
                               coalesce(md.refereeOther,'')
                              ) 
                       ),'refereeInternetSearch',
                         'refereeCompanyColleague',
                         'refereeIndustryPeer',
                         'refereeIndustryEvent',
                         'refereeIndustryPublication',
                         'refereeMarketingEmail',
                         'refereeOther'
      ) as field_name 
FROM
    marketing_details md
WHERE
    md.id = 14588

Huh, I hope I got all the parentheses right!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do i return bit value from a select statement when a column value in the statement is null

From Dev

First column name with non null value by row pandas

From Dev

Select the first column name where a value is null by Id

From Dev

Select the first column name where a value is null by Id

From Dev

Laravel LeftJoin return duplicate value from first column (mysql)

From Dev

Return the name of the MySQL column where a select match was

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

How to select value from second column if first column is blank/null in SQL (MS SQL)?

From Dev

MySQL, select column name based on a value

From Dev

Return the column name as as a value in a column

From Dev

Return JSON from MySQL with Column Name

From Dev

Return JSON from MySQL with Column Name

From Dev

Return Value Column Name

From Dev

Select column name and value from table

From Dev

Invalid Column Name in Select Statement

From Dev

Invalid Column Name in Select Statement

From Dev

Get the column name for the first non-zero value in that row with pandas

From Dev

MySQL - add text prefix before column name in SELECT statement

From Dev

Data Table - Select Value of Column by Name From Another Column

From Dev

For each row return the column index and name of non-NA value

From Dev

Excel: return name of column with highest value in a non-continuous array

From Dev

MySQL SELECT column FROM table WHERE column IS NULL

From Dev

SQL query returning only values from first column in SELECT statement

From Dev

MySQL return max value or null if one column has no value

From Dev

Select COLUMN name from one table based on VALUE from another in mySQL?

From Dev

Mysql select dynamic row values as column name from a table and value from another table

From Dev

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

From Dev

Return the first non-NULL value in the list, or NULL if there are no non-NULL values

Related Related

  1. 1

    How do i return bit value from a select statement when a column value in the statement is null

  2. 2

    First column name with non null value by row pandas

  3. 3

    Select the first column name where a value is null by Id

  4. 4

    Select the first column name where a value is null by Id

  5. 5

    Laravel LeftJoin return duplicate value from first column (mysql)

  6. 6

    Return the name of the MySQL column where a select match was

  7. 7

    MySQL first select column based on parameter value and then value of column

  8. 8

    MySQL first select column based on parameter value and then value of column

  9. 9

    How to select value from second column if first column is blank/null in SQL (MS SQL)?

  10. 10

    MySQL, select column name based on a value

  11. 11

    Return the column name as as a value in a column

  12. 12

    Return JSON from MySQL with Column Name

  13. 13

    Return JSON from MySQL with Column Name

  14. 14

    Return Value Column Name

  15. 15

    Select column name and value from table

  16. 16

    Invalid Column Name in Select Statement

  17. 17

    Invalid Column Name in Select Statement

  18. 18

    Get the column name for the first non-zero value in that row with pandas

  19. 19

    MySQL - add text prefix before column name in SELECT statement

  20. 20

    Data Table - Select Value of Column by Name From Another Column

  21. 21

    For each row return the column index and name of non-NA value

  22. 22

    Excel: return name of column with highest value in a non-continuous array

  23. 23

    MySQL SELECT column FROM table WHERE column IS NULL

  24. 24

    SQL query returning only values from first column in SELECT statement

  25. 25

    MySQL return max value or null if one column has no value

  26. 26

    Select COLUMN name from one table based on VALUE from another in mySQL?

  27. 27

    Mysql select dynamic row values as column name from a table and value from another table

  28. 28

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

  29. 29

    Return the first non-NULL value in the list, or NULL if there are no non-NULL values

HotTag

Archive