Search code examples
mysqlselectcoalesce

SELECT and SELECT COALESCE


I received wonderful help in answering a question yesterday that lead me to successfully use SELECT COALESCE in a MySQL database to replace a nested excel formula. What i'm now struggling with is using the SELECT COALESCE in conjunction with using the SELECT command in the same query. All my columns are coming from the same table (best_estimate) but what I would like to do is:

SELECT 'OFS_ID' from best_estimate

and also perform the following:

SELECT COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL,  0 ) AS my_value
FROM best_estimate

so the end result will result in two columns -

OFS_ID and MY_VALUE

I'm just not sure how to join these two different commands - I spent three hours working on it yesterday and I keep getting syntax errors saying that I can't use SELECT in that place in the query.

Thanks in advance for the help!


Solution

  • Have you tried

    SELECT OFS_ID,COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value FROM best_estimate