英文:
Concat in select formula - snowflake
问题
当我在选择公式中使用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的方式不对吗?
英文:
when I use concat in select formula I will get concat of other column too.
Example:
SELECT
firstname,
surname,
concat(firstname,' ',surname) AS fullname
FROM
employee
Source data:
| firstname | surname |
| John | Kenedy |
Output data:
| firstname | surname | fullname |
| Kenedy John | Kenedy | Kenedy Kenedy John |
Am I using concat wrog way?
答案1
得分: 1
你的语法有问题,这应该可以工作。
SELECT CONCAT(firstname, ' ', surname) as fullname FROM employee;
结果:
+-----------------+
| fullname |
+-----------------+
| John Kenedy |
| Abraham Lincoln |
+-----------------+
你可以在这里获取更多信息。
英文:
Hello you have a bad syntax this must be work
SELECT CONCAT(firstname, ' ', surname) as fullname FROM employee;
Result:
+-----------------+
| fullname |
+-----------------+
| John Kenedy |
| Abraham Lincoln |
+-----------------+
You can get more info here
答案2
得分: 1
better still. don't use concat
function. Use the operator ||
instead. If you use concat()
, and you need to concatenate a bunch of things, it gets very ugly very quickly nesting all the concats within each other.
which do you prefer?
select concat('I ', concat('think ', concat('data ', concat('is ', 'fun '))))
-or-
select 'I ' || 'think ' || 'data ' || 'is ' || 'fun '
英文:
better still. don't use concat
function. Use the operator ||
instead. If you use concat()
, and you need to concatenate a bunch of things, it gets very ugly very quickly nesting all the concats within each other.
which do you prefer?
select concat('I ', concat('think ', concat('data ', concat('is ', 'fun '))))
-or-
select 'I ' || 'think ' || 'data ' || 'is ' || 'fun '
答案3
得分: 0
你的源数据的"firstname"列与输出数据的"firstname"列不相同。如果你按照你所呈现的方式运行你的连接函数,我认为你将获得预期的结果。
编辑1:使用SQL删除记录中的重复单词
- 使用
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
;
备注
- 这不会将任何两个或更多记录与彼此进行比较以查找重复项。
英文:
Your source data firstname column is not the same as your output data firstname column. If you were to run your concat function on the source data as you've presented it, then I believe you would get the results you expect.
Edit 1: Removing duplicate words from a record with SQL
- Use a
SPLIT_TO_TABLE
table function to split each part of the concatenation to an individual row - Use
QUALIFY
clause to filter out duplicate words for each flattened record - Grouping by the firstname and surname, use a
LISTAGG
function to concatenate together each unique word using anORDER BY
clause to preserve the order of the words
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
;
Notes
- This does not compare any two or more records to each other to find duplicates
答案4
得分: 0
Snowflake CONCAT 支持多个参数:
> 连接一个或多个字符串,或连接一个或多个二进制值。如果其中任何值为null,则结果也为null。
>
> CONCAT( <expr1> [ , <exprN> ... ] )
查询按原样工作:
CREATE TABLE employee AS SELECT 'John' AS firstname, 'Kennedy' AS surname;
SELECT firstname,
surname,
concat(firstname,' ',surname) AS fullname
FROM employee;
输出:
FIRSTNAME | SURNAME | FULLNAME |
---|---|---|
John | Kennedy | John Kennedy |
附注: 在过去,CONCAT函数仅支持两个参数。
https://community.snowflake.com/s/ideas - "改进CONCAT函数"
英文:
Snowflake CONCAT supports multiple arguments:
> Concatenates one or more strings, or concatenates one or more binary values. If any of the values is null, the result is also null.
>
> CONCAT( <expr1> [ , <exprN> ... ] )
The query works "as-is":
CREATE TABLE employee AS SELECT 'John' AS firstname, 'Kennedy' AS surname;
SELECT firstname,
surname,
concat(firstname,' ',surname) AS fullname
FROM employee;
Output:
FIRSTNAME | SURNAME | FULLNAME |
---|---|---|
John | Kennedy | John Kennedy |
Sidenote: In the past CONCAT function supported only two arguments.
https://community.snowflake.com/s/ideas - "Improve CONCAT function"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论