在mysql中获取MAX MIN

时间:2015-01-25 10:04:57

标签: mysql sql database greatest-n-per-group

Original Fiddle

Failed attempt

Failed Query

我需要一些帮助来拉动每个品牌在同一行中最昂贵和最便宜的产品,结果应该是这样的:

BRAND_NAME  EX_PRODUCT_ID   EX_NAME EX_PRICE  CHEAP_NAME  CHEAP_PRODUCT_ID  CHEAP_PRICE
HTC         7               Item G  700       Item A         1                500
Sony        5               Item B  100       Item F         2                10
Nokia       4               Item D  260       Item C         3                250

但以下查询与MAX(price)MIN(price)的相应产品不匹配:

BRAND_NAME  EX_PRODUCT_ID   EX_NAME EX_PRICE CHEAP_NAME CHEAP_PRODUCT_ID CHEAP_PRICE
HTC         1               Item A   700     Item A     1                500
Sony        2               Item B   100     Item B     2                10
Nokia       3               Item C   260     Item C     3                250


SELECT t1.brand_name,t1.ex_product_id,t1.ex_name,
       t1.ex_price,
       t2.cheap_name,t2.cheap_product_id,
       t2.cheap_price        
FROM(

  SELECT m.product_id AS ex_product_id,
       m.name AS ex_name,
       MAX(m.price) AS ex_price,
       b.brand_name 
   FROM model m 
   INNER JOIN series s ON s.series_id = m.series_id
   INNER JOIN brand b ON b.brand_id = s.brand_id
   LEFT JOIN (
       SELECT m.product_id,
           m.name,
           MAX(m.price) AS ex_price
        FROM model m   
   )ex ON ex.ex_price = ex_price  /*** problem here*****/
       AND ex.product_id = m.product_id   
   GROUP BY b.brand_id
)t1

JOIN (
  SELECT m.product_id AS cheap_product_id,
         m.name AS cheap_name,
         MIN(m.price) AS cheap_price,
         b.brand_name 
   FROM model m 
   INNER JOIN series s ON s.series_id = m.series_id
   INNER JOIN brand b ON b.brand_id = s.brand_id
   INNER JOIN(
     SELECT m.product_id,
           m.name,
           MIN(m.price) AS ch_price
        FROM model m   

   )ch  ON ch.ch_price = cheap_price   /*** problem here*****/
        AND ch.product_id = m.product_id 
   GROUP BY b.brand_id
)t2 ON t2.brand_name = t1.brand_name


LIMIT 10

有人能指出我如何将每个品牌的最高和最低价格与相应的昂贵和便宜的产品相匹配?我的查询显然无法识别on子句中的MAXMIN列。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您可以使用条件聚合来完成此操作。只需在子查询中找到最小和最大价格,然后将数据拉出来:

  SELECT b.brand_name,
         minmax.price_max,
         max(case when m.price = minmax.price_max then m.product_id end) as max_product_id,
         max(case when m.price = minmax.price_max then m.name end) as max_name,
         minmax.price_min,
         max(case when m.price = minmax.price_min then m.product_id end) as min_product_id,
         max(case when m.price = minmax.price_min then m.name end) as min_name
   FROM model m INNER JOIN
        series s
        ON s.series_id = m.series_id INNER JOIN
        brand b
        ON b.brand_id = s.brand_id INNER JOIN
        (SELECT m.product_id, m.name,
                MIN(m.price) AS price_min,
                MAX(m.price) AS price_max
        FROM model m   
       ) minmax
       ON m.price IN (minmax.price_min, minmax.proc_max) AND 
          minmax.product_id = m.product_id   
   GROUP BY b.brand_id

请注意,如果有多个产品具有相同的最低或最高价格,则会拉出任意一个。您可以使用group_concat()代替max()来获取所有这些内容。