Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

sql - Oracle return same alias for different cases on select

What I am trying to do is based on a condition (value of a column) to return one or multiple columns.

I always need my query to return two columns: Article, Paragraph

If the value of a column list_value which I am checking is Article, then I only need to return the Article and null to Paragraph.

If the value of a column is Paragraph, I need to return both paragraph and article but the article's value now returns from a subquery based on the parent column value.

Table pro_table, Data:

enter image description here

SELECT
     tbl.parent,
     list.list_value,
     tbl.values_column,
     CASE
         WHEN list.list_value = 'Article' THEN tbl.values_column
     END AS article,
     CASE
         WHEN list.list_value = 'Paragraph' THEN tbl.values_column
     END AS paragraph,
     CASE
         WHEN list.list_value = 'Paragraph' THEN (
             SELECT
                 values_column
             FROM
                 pro_table
             WHERE
                 pro_table.id_pro_table = tbl.parent
         )
     END AS article
 FROM
     pro_table tbl 
     LEFT JOIN list ON list.id_list = tbl.id_t_list
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming your subquery already gets the correct value back, and you're just asking how to get both 'article' values in the same column, all you need to do is move the subquery into a second when check within the first case expression, instead of as its own column:

SELECT
     tbl.parent,
     list.list_value,
     tbl.values_column,
     CASE
         WHEN list.list_value = 'Article' THEN tbl.values_column
         WHEN list.list_value = 'Paragraph' THEN (
             SELECT
                 values_column
             FROM
                 pro_table
             WHERE
                 pro_table.id_pro_table = tbl.parent
         )
     END AS article,
     CASE
         WHEN list.list_value = 'Paragraph' THEN tbl.values_column
     END AS paragraph
 FROM
     pro_table tbl 
     LEFT JOIN list ON list.id_list = tbl.id_t_list

You could replace the subquery with a left join back to the same table and coalesce the results:

SELECT
     tbl.parent,
     list.list_value,
     tbl.values_column,
     COALESCE (
         CASE
             WHEN list.list_value = 'Article' THEN tbl.values_column
         END,
         tbl2.values_column
     ) AS article,
     CASE
         WHEN list.list_value = 'Paragraph' THEN tbl.values_column
     END AS paragraph
 FROM
     pro_table tbl 
     LEFT JOIN list ON list.id_list = tbl.id_t_list
     LEFT JOIN pro_table tbl2 ON tbl2.id_pro_table = tbl.parent

... but that's making some assumptions about the hierarchy, so you probably need to tighten the join up based on the list type.

db<>fiddle with invented relationships between the same six rows you showed, as we don't have the actual IDs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...