SQL将字段合并为一个结果(SQL unite fields to one result)

编程入门 行业动态 更新时间:2024-10-24 12:22:10
SQL将字段合并为一个结果(SQL unite fields to one result)

我知道这是一个“不是内置的”或者“dba认为的方式”,而是一种程序员的方法,如何从3个字段请求一个非空的请求到一个结果字段。

假设我们有一个f1,f2,f3,f4,f5.的表格f1,f2,f3,f4,f5.

假设f2,f3,f4是相同的类型。

假设表格的内容是元组的

(key1,null,null,value1,value2) (key2,null,value3,value4,value5) (key3,null,null,null,value6)

现在,如果我们返回第一个元组,那么我们得到(key1)(key1,value1,value2)

如果我们要求key2,我们得到(key1,value3,value5)

如果我们要求key3,我们得到(key1,null,value6)

如何获得字段的优先级,如果你有f2的值,那么它的设置到返回的字段,只有那么如果我们在f3的值,然后它的设置到中间返回的字段,只有如果我们有价值在f4中,然后将其置入中间返回的字段

主要目标是将结果放入单个字段并防止结果结束时所需的开销工作。

I know this is a "not build in" or "the way dba thinks" but a programmer approach, how could one request from 3 fields to get the one that is not null, into a result field.

Let's say we have a table with f1,f2,f3,f4,f5.

Let's say f2,f3,f4 are the same type.

Let's say the content of the table be tuples of

(key1,null,null,value1,value2) (key2,null,value3,value4,value5) (key3,null,null,null,value6)

Now if we return the first tuple then we get (key1) we get (key1,value1,value2)

If we ask for key2 we get (key1,value3,value5)

If we ask for key3 we get (key1,null,value6)

How is it possible to get the fields in the priority of if you have value in f2, then its set into the returned field, only then if we have value in f3 then its set into the middle returned field, only then if we have value in f4 then its set into the middle returned field

The main goal is to get the result into a single field and prevent the overhead work needed at the result end.

最满意答案

正如Bernd_k所建议的, COALESCE是你的朋友

SELECT [key], COALESCE(f2, f3, f4), f5 FROM YourTable

As Bernd_k suggested, COALESCE is your friend here

SELECT [key], COALESCE(f2, f3, f4), f5 FROM YourTable

更多推荐

本文发布于:2023-07-22 18:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   并为   SQL   result   fields

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!