当我在选择公式中使用concat时,我也会获得其他列的concat。
示例:
SELECT
firstname,
surname,
concat(firstname,' ',surname) AS fullname
FROM
employee
源数据:
| firstname | surname |
| John | Kenedy |
输出数据:
| firstname | surname | fullname |
| Kenedy John | Kenedy | Kenedy Kenedy John |
我使用concat wrog方式吗?
答案 0 :(得分:1)
你好,你的语法不好,这一定可以解决
SELECT CONCAT(firstname, ' ', surname) as fullname FROM employee;
结果:
+-----------------+
| fullname |
+-----------------+
| John Kenedy |
| Abraham Lincoln |
+-----------------+
您可以获得更多信息here
答案 1 :(得分:0)
您的源数据名列与您的输出数据名列不同。如果您要在显示时对源数据运行concat函数,那么我相信您会获得预期的结果。
SPLIT_TO_TABLE
表函数将串联的每个部分拆分为单独的行QUALIFY
子句为每个拼合记录过滤出重复的单词LISTAGG
函数通过ORDER BY
子句将每个唯一单词连接在一起,以保留单词的顺序CREATE OR REPLACE TEMPORARY TABLE TMP_EMPLOYEE
AS
SELECT $1 AS FIRSTNAME
,$2 AS SURNAME
FROM VALUES
('John','Kenedy')
,('Kenedy John','Kenedy')
;
WITH A AS (
SELECT E.FIRSTNAME
,E.SURNAME
,STT.SEQ
,STT.INDEX
,STT.VALUE
FROM TMP_EMPLOYEE E
,LATERAL SPLIT_TO_TABLE(FIRSTNAME || ' ' || SURNAME,' ') STT
QUALIFY ROW_NUMBER() OVER(PARTITION BY STT.SEQ,STT.VALUE ORDER BY STT.INDEX) = 1
)
SELECT A.FIRSTNAME
,A.SURNAME
,LISTAGG(A.VALUE,' ') WITHIN GROUP(ORDER BY A.INDEX) AS FULLNAME
FROM A
GROUP BY A.FIRSTNAME,A.SURNAME
;
注释
答案 2 :(得分:0)
更好。不要使用StreamTransformer
函数。请改用运算符concat
。如果您使用||
,并且需要串联一堆东西,那么将所有concat嵌套在彼此之间很快就会变得非常难看。
您更喜欢哪个?
concat()
-或-
select concat('I ', concat('think ', concat('data ', concat('is ', 'fun '))))