Skip to content
Advertisement

Split String and Count Teradata SQL

I would like to split column ‘ASPCT_VLU_NM’ and count of occurrences of the timestamps in a given time frame. For example return the ID which have more than 3 occurrence in between 1537160520286 and 1537190520286.

In the example below those are the timestamps :
The values which should be extracted from the column

The data in Teradata

Advertisement

Answer

If you are using Teradata 14 or later, then you may take advantage of the STRTOK function here:

SELECT
    ID
FROM yourTable
WHERE
    CAST(STRTOK(ASPCT_VLU_NM, ',', 4) AS BIGINT) BETWEEN 1537160520286 AND 1537190520286
GROUP BY
    ID
HAVING
    COUNT(*) > 3;

Note: You may want to give serious consideration to changing your table design, in particular to not storing CSV data in the ASPCT_VLU_NM column. Instead, get each CSV value into a separate row/column.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement