0

我一直在尝试将单个列中存在的一组值(就像上面一样)分类为数据类型。问题是我正在使用 Aster SQL 环境(功能的可用性和整个环境非常有限)。另一个问题是列中有很多垃圾值、很多符号、字符等,这使得甚至很难对问题进行硬编码。结构类似于:

FeatureValue
123
24
15.6
17:15
abc
12/18/2014
17/222222           
abc1200                                
001001oo             
positve+              
+1                           

我希望解决方案是 SQL 查询。最终结果应该是这样的:

FeatureValue    Type
123             Numeric
24              Numeric
15.6            Numeric
17:15           String (?time)
abc             String
12/18/2014      Date
17/222222       String
abc1200         String
001001oo        String
positve+        String
+1              String

我编码了一点,但是这个解决方案不是很可靠。我所做的是:

case 
            when upper(trim(feature_value)) not like '%A%' and
            upper(trim(feature_value)) not like '%B%' and
            upper(trim(feature_value)) not like '%C%' and
            upper(trim(feature_value)) not like '%D%' and
            upper(trim(feature_value)) not like '%E%' and
            upper(trim(feature_value)) not like '%F%' and
            upper(trim(feature_value)) not like '%G%' and
            upper(trim(feature_value)) not like '%H%' and
            upper(trim(feature_value)) not like '%I%' and
            upper(trim(feature_value)) not like '%J%' and
            upper(trim(feature_value)) not like '%K%' and
            upper(trim(feature_value)) not like '%L%' and
            upper(trim(feature_value)) not like '%M%' and
            upper(trim(feature_value)) not like '%N%' and
            upper(trim(feature_value)) not like '%O%' and
            upper(trim(feature_value)) not like '%P%' and
            upper(trim(feature_value)) not like '%Q%' and
            upper(trim(feature_value)) not like '%R%' and
            upper(trim(feature_value)) not like '%S%' and
            upper(trim(feature_value)) not like '%T%' and
            upper(trim(feature_value)) not like '%U%' and
            upper(trim(feature_value)) not like '%V%' and
            upper(trim(feature_value)) not like '%W%' and
            upper(trim(feature_value)) not like '%X%' and
            upper(trim(feature_value)) not like '%Y%' and
            upper(trim(feature_value)) not like '%Z%' and       
            upper(trim(feature_value)) <>'' and
            upper(trim(feature_value)) not like '%+%' and 
            upper(trim(feature_value)) is not null and
            --upper(trim(feature_value))<>'-' and 
            upper(trim(feature_value))<>'NULL' and 
            upper(trim(feature_value)) not like '%/%' and 
            upper(trim(feature_value)) not like '%-%' and 
            upper(trim(feature_value)) not like '%:%' and 
            feature_value is not null 
                then 'NUMERIC'           
            else 'STRING'
        end as value_type
4

1 回答 1

2

您可以尝试使用 LIKE 语句中的字符范围来控制 CASE 噩梦:

CASE WHEN upper(trim(feature_value)) NOT LIKE '%[A-Z/-+:]%'
    AND upper(trim(feature_value)) NOT LIKE ''
    AND upper(trim(feature_value)) IS NOT NULL
    THEN 'NUMERIC'
    ELSE 'STRING'
END AS value_type

根据需要修改/扩展。

于 2014-12-18T14:34:55.320 回答