I have strings in vertica database like:
'a18: 2, b34: 5, n29: 10'
and i need to extracts digits from it and get sum. So the output should be:
17
(calculated 2+5+10)
I need it for working in Tableau. How can i do it?
I have strings in vertica database like:
'a18: 2, b34: 5, n29: 10'
and i need to extracts digits from it and get sum. So the output should be:
17
(calculated 2+5+10)
I need it for working in Tableau. How can i do it?
While modifying the data model will make your life a lot easier, Your scenario can be handled using Tableau calculations alone.
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 ) ))))
Here ZN replaces NULL with zero so that we dont have to worry about missing splits. We are simply taking alternate fields and converting them to integers.
Here is the final result:
Two steps, using the SPLIT_PART
function, I think:
a) Verticalise your string into rows.
b) split your resulting strings by the colon, cast the token after the colon as an integer, and make a sum of those integers.
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
;