分组依据和在同一选择中排序

时间:2014-02-13 09:00:55

标签: sql oracle

我有这个问题:

  SELECT MODELLI.MOD_DESC, 
         COUNT(MOD_DESC)
    FROM VEGA.DISTRIB AS DISTRIB, 
         VEGA.MODELLI AS MODELLI
   WHERE DISTRIB.DIS_FLAG = 'M' 
     AND DISTRIB.DIS_MOD = MODELLI.MOD_COD
   GROUP BY MODELLI.MOD_DESC

但我也希望在MODELLI.MOD_DESC上有ORDER BY。

我怎么能这样做?

感谢。

3 个答案:

答案 0 :(得分:2)

尝试,

SELECT MODELLI.MOD_DESC, COUNT(MODELLI.MOD_DESC)
FROM VEGA.DISTRIB DISTRIB, VEGA.MODELLI MODELLI
WHERE DISTRIB.DIS_FLAG = 'M' AND DISTRIB.DIS_MOD = MODELLI.MOD_COD
GROUP BY MODELLI.MOD_DESC ORDER BY MODELLI.MOD_DESC

它会像OP建议的那样工作..

立即尝试..

答案 1 :(得分:2)

您可以在ORDER BY子句之后使用GROUP BY子句,例如

SELECT MODELLI.MOD_DESC, 
       COUNT(MOD_DESC)
FROM   VEGA.DISTRIB DISTRIB, 
       VEGA.MODELLI MODELLI
WHERE  DISTRIB.DIS_FLAG = 'M' 
AND    DISTRIB.DIS_MOD = MODELLI.MOD_COD
GROUP BY MODELLI.MOD_DESC
ORDER BY MODELLI.MOD_DESC;

答案 2 :(得分:1)

SELECT MODELLI.MOD_DESC, COUNT(MOD_DESC)
FROM VEGA.DISTRIB AS DISTRIB, VEGA.MODELLI AS MODELLI
WHERE DISTRIB.DIS_FLAG = 'M' AND DISTRIB.DIS_MOD = MODELLI.MOD_COD
GROUP BY MODELLI.MOD_DESC
order by MODELLI.MOD_DESC ASC