5

以下のようなテーブルがあります。

単一のクエリで最小、最大、平均コストの製品のproduct_idが必要です。

CREATE TABLE productlist(product_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                         cost INT);                             

INSERT INTO productlist(cost)
                 VALUES('2450'),
                       ('2200'),
                       ('2580'),
                       ('2405'),
                       ('3500'),
                       ('1500'),
                       ('1800'),
                       ('1520'),
                       ('1740'),
                       ('1940'),
                       ('2940'),
                       ('1250'),
                       ('1290'),
                       ('1390'),
                       ('2900');

出力:

Min    12
Max    5
Avg    2093

以下のように試しましたが、機能しません。

SELECT product_id, MIN(cost) as mincost
  FROM productlist
 GROUP BY product_id
 ORDER BY mincost ASC
 LIMIT 0,1
 UNION
SELECT product_id, max(cost) as maxcost
  FROM productlist
 GROUP BY product_id
 ORDER BY maxcost DESC
 LIMIT 0,1

どうすればいいですか

4

5 に答える 5

1
select product_id, cost
from productlist where cost = (SELECT max(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT min(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT x.cost from productlist x, productlist y
GROUP BY x.cost
HAVING SUM(SIGN(1-SIGN(y.cost-x.cost))) = (COUNT(*)+1)/2)

これは中央値を使用し、すべての場合に製品IDを返します

于 2012-12-31T07:33:18.067 に答える
1
select 'Min', product_id 
from productlist
where cost = (select min(cost) from productlist)
UNION 
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION 
select 'Avg', round(AVG(cost),0) as Avg
from productlist
于 2012-12-31T07:33:55.927 に答える
1

あなたが書いたクエリによってあなたが望む出力が来ていないあなたは必要な出力を得るためにこれを試す必要があります

select 'Min', product_id 
from productlist
where cost = (select min(cost) from productlist)
UNION 
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION 
select 'Avg', floor(AVG(cost)) as Avg
from productlist
于 2012-12-31T07:35:43.867 に答える
0

これは、すべての製品を選択することです

  SELECT 
          max(cost), MIN(cost), AVG(cost)
    FROM 
          productlist
    GROUP BY
          product_id

ここでは、GROUPBYは必ずしも必要ではありません。しかし、あなたは初心者のようです、グーグルすることはあなたを助けるでしょう。

あなたの質問のためにこれを試してください。

SELECT 
          (select CONCAT(product_id, '-', cost) from  productlist group by product_id order by cost DESC limit 1) as MAX,
          (select CONCAT(product_id, '-', cost) from  productlist group by product_id order by cost ASC limit 1) as MIN,
          (select avg(cost) from  productlist) as AVG
FROM 
           productlist limit 1
于 2012-12-31T07:26:43.267 に答える
0

これはあなたの質問に正確に答えますが、このデータを見つけるには3回のテーブルスキャンが必要であることに注意してください。また、質問の例は、平均値が2093.67から2093に切り捨てられることを示しています。これをroundに置き換える方がおそらく良いでしょう。

SQLフィドル

SELECT concat('Min ', product_id) 
  FROM productlist
 WHERE cost = (SELECT min(cost) from productlist)
UNION ALL
SELECT concat('Max ', product_id) 
  FROM productlist
 WHERE cost = (SELECT max(cost) from productlist)
UNION ALL
SELECT concat('Avg ', truncate(avg(cost), 0)) 
  FROM productlist
于 2012-12-31T07:32:41.153 に答える