0

can i get 'colNew_name' from a select query like table below?

original table:

category_name | col1_name  | col2_name  | 
:------------ | :--------- | :----------|
postgres      | 1          | null       |
SAM           | null       | 1          |
MariaDB       | 1          | 1          |

result table:

category_name | col1_name  | col2_name  |      colNew_name   |
:------------ | :--------- | :----------| :------------------|
postgres      | 1          | null       |     col1_name      |
SAM           | null       | 1          |     col2_name      |
MariaDB       | 1          | 1          | col1_name,col2_name|
1

If your list of columns is small and fixed you could always do:

SELECT *, 
       CONCAT_WS(',', 
                 CASE WHEN col1_name IS NULL THEN NULL ELSE 'col1_name' END,
                 CASE WHEN col2_name IS NULL THEN NULL ELSE 'col2_name' END)
       AS "colNew_name"
FROM table1

See http://sqlfiddle.com/#!15/a88af8/2

3
  • thanks ...but list is around 42 columns, any other suggestions? – Data Analyst IA May 31 '18 at 17:22
  • Give all your real contraints in the body of your question, if you want answers tailored to your situation. Your original table example has only 2 columns... so difficult to imagine that your real life case has 42 columns... – Patrick Mevzek May 31 '18 at 17:26
  • I wrote this large query and it works for my ,in future questions i'll add all constraints,...thanks again – Data Analyst IA May 31 '18 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.