SQL DB2:合并 3 列输出的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15251909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DB2: Function to merge 3 column output
提问by Deepak
I have below records in table1
我在表1中有以下记录
c1 c2 c3
----------
A B C
How to merge c1 c2 and c3 so the output would like
如何合并 c1 c2 和 c3 以便输出
A B C with space in between the output I used concat function but its not taking 3 arguments like
输出之间有空格的 ABC 我使用了 concat 函数,但它没有采用 3 个参数,例如
select concat (c1,c2,c3) from table1
I cant run select * from table1
as I want output in one column
我不能运行,select * from table1
因为我想在一列中输出
回答by GilShalit
This works in z/OS versions at least:
这至少适用于 z/OS 版本:
select c1 concat ' ' concat c2 concat ' ' concat c3
Get to know the DB2 documentation
了解 DB2文档
回答by Dave
I came across the same problem recently, I used the || (double pipes) in order to concat to columns.
我最近遇到了同样的问题,我使用了 || (双管)以连接到列。
I also had to write a wrapper to in my query to overcame the issue.
我还必须在我的查询中编写一个包装器来解决这个问题。
Below is a snippet of what my queries looked like in the end.
下面是我的查询最终是什么样子的片段。
select a1 || a2 as a2, a3 || a4 as a4 --wrapper 2
from (
select '"service":"' as a1,a2, '","total":' as a3, a4 --wrapper 1
from (
select distinct(a2),count(*) as a4
from abc.table
group by a2
order by a2)
);
Below is what the ouput was from the query:
以下是查询的输出:
"service":"ABC" , "total":123
回答by Mortalus
try this.
尝试这个。
select concat(concat (c1,c2),c3) from table1
回答by Kevin H
I had a issue with converting from SQL to DB2. This page helped, but I ended up making a little change:
我在从 SQL 转换为 DB2 时遇到了问题。这个页面有帮助,但我最终做了一些改变:
SELECT
RTRIM(C1) || '' || C2 as CFULL
FROM TABLE
回答by user7615535
SELECT C1 CONCAT '' CONCAT ( C2 CONCAT '' CONCAT C3) FROM TABLENAME
SELECT C1 CONCAT '' CONCAT ( C2 CONCAT '' CONCAT C3) FROM TABLENAME