sql;将 MIN(price) 和 MAX(price) 连接到列中?
sql; concatenate MIN(price) and MAX(price) into column?
我正在处理 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 的情况,其中最高价格和最低价格相同)则只显示一个值而不是具有相同 max/min 值的价格范围?
这最后一部分是一个延伸目标,首先我只是想创建 price_range 列
使用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
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
我正在处理 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 的情况,其中最高价格和最低价格相同)则只显示一个值而不是具有相同 max/min 值的价格范围?
这最后一部分是一个延伸目标,首先我只是想创建 price_range 列
使用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
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