Is it possible to replicate a string by a specified number of times with bigquery? Here's a toy example of what I would like to achieve ...
Input:
this.fetch(...)
Replicate id by count (i.e. desired output):
id | count
----------
a | 1
b | 2
c | 3
It seems like this functionality is provided by id | newstr
----------
a | a
b | bb
c | ccc
in other sql variants.
答案 0 :(得分:4)
您不需要UDF。无论如何它会慢得多。只需使用SQL函数REPEAT
(参见here):
#standardSQL
WITH
data AS (
SELECT
'a' AS id,
2 AS repititions
UNION ALL
SELECT
'b' AS id,
3 AS repititions)
SELECT
id,
repititions,
REPEAT(id,
repititions) AS repeated
FROM
data
答案 1 :(得分:1)
我认为BigQuery中没有REPLICATE()函数,但您可以像这样创建UDF函数:
#standardSQL
CREATE TEMPORARY FUNCTION REPLICATE(x STRING, y FLOAT64)
RETURNS STRING
LANGUAGE js AS """
return x.repeat(y);;
""";
WITH data AS (SELECT 'a' as id, 2 as count),
data1 AS (SELECT 'b' as id, 3 as count)
SELECT id, REPLICATE(id,count) as newstr FROM data
UNION ALL
SELECT id, REPLICATE(id,count) as newstr FROM data1
在这种情况下输出将是:
Row id newstr
1 a aa
2 b bbb