【问题标题】:sql; concatenate MIN(price) and MAX(price) into column?sql;将 MIN(price) 和 MAX(price) 连接到列中?
【发布时间】:2020-07-20 17:19:04
【问题描述】:

我正在处理这样的 aws athena 查询:

SELECT 
    normalised_brand, 
    COUNT(DISTINCT merch1) merch1_distinct_count,
    COUNT(DISTINCT category_level_1) category_level_1_distinct_count,
    COUNT(*) product_distinct_count,
    MIN(effective_price) maxprice, 
    MAX(effective_price) minprice 
    -- CONCAT_WS(' - ', minprice, maxprice) price_range
    
FROM "db" 
WHERE product_gap = 'yes' AND store_name = 'petco'
group by normalised_brand

这将给出如下结果:

现在我将 maxprice 和 minprice 作为两个单独的列,但我想将它们组合成一列“价格范围”,其中 minprice 和 maxprice 由“-”字符串分隔,所以看起来像这样:

price_range
3.99 - 5.33
2.11 - 9.99
2.22 - 2.22

我尝试通过添加 CONCAT 来做到这一点,但没有得到有效的结果。我对 sql 非常陌生,我想知道如何通过将 MAX() 和 MIN() 结果组合成一个字符串值来创建这个价格范围列。

另外,我可以向 SQL 添加逻辑,以便如果价格范围为零(例如 2.22 - 2.22 的情况,其中最高价格和最低价格相同),则只显示单个值而不是价格范围相同的最大值/最小值?

这最后一部分是一个延伸目标,首先我只是试图创建 price_range 列

【问题讨论】:

    标签: sql presto amazon-athena


    【解决方案1】:

    使用concat()

    SELECT 
        normalised_brand, 
        COUNT(DISTINCT merch1) merch1_distinct_count,
        COUNT(DISTINCT category_level_1) category_level_1_distinct_count,
        COUNT(*) product_distinct_count,
        MIN(effective_price) maxprice, 
        MAX(effective_price) minprice 
        concat(cast(MIN(effective_price) as varchar(10)),'-',cast(MAX(effective_price) as varchar(10)))
    FROM "db" 
    WHERE product_gap = 'yes' AND store_name = 'petco'
    group by normalised_brand
    

    【讨论】:

    • 尝试添加该行但出现错误:line 8:5: extraneous input 'concat' expecting {<eof>, ',', 'from', 'where', 'group', 'order', 'having', 'limit', 'union', 'except', 'intersect'} (service: amazonathena; status code: 400; error code: invalidrequestexception;
    • 我尝试在 cast 语句的末尾再添加几个 ')' 括号,因为它似乎缺少一个:concat( cast(MIN(effective_price) as varchar(10)), '-', cast(MAX(effective_price) as varchar(10)), ) 同样的问题
    • @Martin,我已经更新了答案 - 缺少括号
    • 谢谢,我在 aws athena 中运行它,处理这个与新 concat() 行相关的错误extraneous input 'concat' expecting {<eof>, ',', 'from', 'where', 'group', 'order', 'having', 'limit', 'union', 'except', 'intersect'} (service: amazonathena; status code: 400; error code: invalidrequestexception;,我将尝试使用其他一些值测试 concat
    • 我明白了,必须将演员表达式放在括号中,如下所示:CONCAT(CAST(MIN(effective_price) as varchar(10)),' - ', CAST(MAX(effective_price) as varchar(10))) 谢谢
    【解决方案2】:
    SELECT 
        normalised_brand, 
        COUNT(DISTINCT merch1) merch1_distinct_count,
        COUNT(DISTINCT category_level_1) category_level_1_distinct_count,
        COUNT(*) product_distinct_count,
        MIN(effective_price) maxprice, 
        MAX(effective_price) minprice, 
        CONCAT( MIN(effective_price),'-' , MAX(effective_price)) price_range
        
    FROM "db" 
    WHERE product_gap = 'yes' AND store_name = 'petco'
    group by normalised_brand
    

    http://sqlfiddle.com/#!9/85668/44

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-30
      • 2017-03-15
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多