获取无法识别的数值

Getting Numeric value not recognized

 SELECT ID,INDEX,purchase_list,
      NVL(CASE WHEN INDEX = 2 THEN purchase_list END,0)
             AS qty,
sum(NVL(CASE WHEN INDEX = 2 THEN purchase_list END,0))over(partition by ID) as order_qty    
  FROM (
        SELECT ID,index,d.value::string AS purchase_list FROM (
                        SELECT ID,c.value::string AS purchase_list
                          FROM table_1,LATERAL flatten(INPUT=>split(post_purchase_list, '|')) c 
                      ), LATERAL flatten(INPUT=>split(purchase_list, ';')) d
       )WHERE ID=1250707

当我运行上述查询时,我得到

Numeric value is not recognized.

列 - post_purchase_list 是 VARCHAR 列。

SELECT 'this is a string' as this_is_a_str,
    NVL(this_is_a_str, 0) as one_of_these_is_str_and_the_other_is_a_number;

还给出:

Numeric value 'this is a string' is not recognized

也就是说,您的类型不同,ether 将您的“字符串转换为一个数字,如果它是一个数字”或将您的零转换为一个字符串 '0'

SELECT '1234' as this_is_a_str,
    0 as this_is_a_number,
    NVL(this_is_a_str, this_is_a_number::string),
    NVL(this_is_a_str::int, this_is_a_number);

这很好用。现在,对于您的代码,您要执行两次。

SELECT 
    ID,
    INDEX,
    purchase_list,
    NVL(CASE WHEN INDEX = 2 THEN purchase_list END, '0') AS qty,
    sum(NVL(CASE WHEN INDEX = 2 THEN purchase_list END, '0')) over (partition by ID) as order_qty    
FROM (
    SELECT 
        ID,
        index,
        d.value::string AS purchase_list 
    FROM (
        SELECT 
            ID,
            c.value::string AS purchase_list
        FROM table_1,
            LATERAL flatten(INPUT=>split(post_purchase_list, '|')) c 
    ), LATERAL flatten(INPUT=>split(purchase_list, ';')) d
)
WHERE ID = 1250707

但如前所述,可以写出这些扁平化

SELECT 
    ID,
    INDEX,
    purchase_list,
    NVL(CASE WHEN INDEX = 2 THEN purchase_list END,'0') AS qty,
    sum(NVL(CASE WHEN INDEX = 2 THEN purchase_list END,'0')) over (partition by ID) as order_qty    
FROM (
    SELECT 
        ID,
        d.index,
        d.value::string AS purchase_list 
    FROM table_1
        ,LATERAL flatten(INPUT=>split(post_purchase_list, '|')) c 
        ,LATERAL flatten(INPUT=>split(c.value::string, ';')) d
)
WHERE ID = 1250707

并且 qty 可以重复使用:

SELECT 
    ID,
    INDEX,
    purchase_list,
    NVL(CASE WHEN INDEX = 2 THEN purchase_list END,'0') AS qty,
    sum(qty) over (partition by ID) as order_qty    
FROM (
    SELECT 
        ID,
        d.index,
        d.value::string AS purchase_list 
    FROM table_1
        ,LATERAL flatten(INPUT=>split(post_purchase_list, '|')) c 
        ,LATERAL flatten(INPUT=>split(c.value::string, ';')) d
)
WHERE ID = 1250707

SELECT 
    ID,
    d.index,
    d.value::string AS purchase_list, 
    NVL(CASE WHEN d.index = 2 THEN purchase_list END, '0') AS qty,
    sum(qty) over (partition by ID) as order_qty 
FROM table_1
    ,LATERAL flatten(INPUT=>split(post_purchase_list, '|')) c 
    ,LATERAL flatten(INPUT=>split(c.value::string, ';')) d
WHERE ID = 1250707