我在vertica数据库中有如下字符串:
'a18: 2, b34: 5, n29: 10'
,我需要从中提取数字并求和。 因此输出应为:
17
(计算2 + 5 + 10)
在Tableau中工作时需要它。 我该怎么办?
答案 0 :(得分:1)
虽然修改数据模型将使您的生活更加轻松,但是可以仅使用Tableau计算来处理您的情况。
ZN(INT((TRIM( SPLIT( [Formatted String], ",", 2 ) ))))+ ZN(INT((TRIM( SPLIT( [Formatted String], ",", 4 ) ))))+ ZN(INT((TRIM( SPLIT( [Formatted String], ",", 6 ) ))))+ ZN(INT((TRIM( SPLIT( [Formatted String], ",", 8 ) ))))+ ZN(INT((TRIM( SPLIT( [Formatted String], ",", 10 ) ))))
此处ZN将NULL替换为零,因此我们不必担心丢失拆分。我们只是将备用字段转换为整数。
这是最终结果:
答案 1 :(得分:0)
我认为使用SPLIT_PART
函数可以执行两个步骤:
a)将字符串垂直排列成行。
b)用冒号分隔结果字符串,将冒号后面的标记转换为整数,然后将这些整数相加。
WITH
-- this is your single-row input
-- if you have many rows, you will have to add any grouping column here
input(s) AS (
SELECT 'a18: 2, b34: 5, n29: 10'
)
,
-- start real WITH clause here ...
i(i) AS (
-- create 10 "index" integers out of nothing using TIMESERIES ...
SELECT
MICROSECOND(ts) AS i
FROM ( SELECT TIMESTAMPADD(MICROSECOND, 1, '2000-01-01')
UNION ALL SELECT TIMESTAMPADD(MICROSECOND, 10, '2000-01-01')
) limits(ts_lim)
TIMESERIES ts AS '1 MICROSECOND' OVER (ORDER BY ts_lim)
)
,
-- verticalise your comma separated string
rows_obtained AS (
SELECT
TRIM(SPLIT_PART(s,',',i)) AS row_obtained
FROM input CROSS JOIN i
WHERE SPLIT_PART(s,',',i) <> '' -- remove empty strings
-- you get:
--+ row_obtained
--+ a18: 2
--+ b34: 5
--+ n29: 10
)
-- get the sum of the second part of SPLIT_PART() by colon, cast to integer
SELECT
SUM(SPLIT_PART(row_obtained,':',2)::INT) AS the_sum
FROM rows_obtained
;