0

最も使用されている RAM 構成で各モデルのクエリを見つけようとしています。

テーブル:

PC (code, model, speed, ram, hd, cd, price)

これまでのところ、すべてのモデルをすべての RAM 構成とその RAM 構成が使用された回数とともにリストすることができました。

select model, ram, max(config)
  from (select model,ram,count(ram) as config
          from pc 
         group by model, ram)
 group by model, ram

出力:

MODEL   RAM  MAX(CONFIG)
------- ---- -----------
1232    64   2
1232    32   2
1233    128  3
1121    128  3
1233    64   1
1260    32   1

モデルを最も使用されている RAM とともにリストに表示しようとすると、問題が発生します。

select model, ram
  from (select model, ram, count(ram) as config
          from pc 
         group by model, ram)
 group by model
having config = max(config);


Error : ORA-00979: not a GROUP BY expression
4

3 に答える 3

0

あなたが探しているのは次のとおりだと思います:

SELECT model,ram FROM (SELECT model,ram,count(ram) AS config
FROM pc 
GROUP BY model,ram)
WHERE config=max(config)

レコードは、サブクエリによって既にグループ化されている必要があります

于 2015-08-21T21:19:51.987 に答える
0
with x as 
(select model,ram,count(ram) as config
from pc 
group by model,ram)
, y as 
(select model, max(config) as mxconfig from x group by model)
select x.model, x.ram --choose max(x.ram) or min(x.ram) in case of a tie and group by x.model
from x join y on x.model = y.model and x.config = y.mxconfig

このソリューションはcte、必要なものを達成するために使用します。max構成に関係がある場合にいずれかまたはminramを取得する必要がある場合は、group byモデルにもう 1 つ必要です。

于 2015-08-21T21:18:34.773 に答える