我怎样才能得到这张桌子的平均值?

时间:2015-11-19 09:47:17

标签: sql oracle

我有两栏

SQL> SELECT COUNT(A),CAR                                                        
  2  FROM ASIGNATURA                                                            
  3  GROUP BY CAR;                                                              

COUNT(A)        CAR

 1          1
 1          6
 1          2
 2          4
 1          5
 1          8
 4          7
 1          3

选择了8行。

SQL> SELECT AVG(COUNT(A)),CAR                                                   
2  FROM ASIGNATURA                                                            
3  GROUP BY CAR;                                                              


SELECT AVG(COUNT(A)),CAR                     *
ERROR at line 1:
ORA-00937: not a single-group group function

我希望两列的平均值如下:

 1/12   1
 1/12   6
 1/12   2
 2/12   4

 etc... 

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您实际上并未计算平均值,而是将其显示为平均值。

尝试:

SELECT COUNT(CAR)
  ||'/'
  ||
  (SELECT COUNT(*) AS TOTAL FROM ASIGNATURA
  ) cnt_avg,
  CAR
FROM ASIGNATURA
GROUP BY CAR;
使用SCOTT模式中的标准EMP表

演示

SQL> SELECT COUNT(deptno)
  2    ||'/'
  3    ||
  4    (SELECT COUNT(*) AS TOTAL FROM emp
  5    ) cnt_avg,
  6    deptno
  7  FROM emp
  8  GROUP BY deptno;

CNT_AV     DEPTNO
------ ----------
6/14           30
5/14           20
3/14           10

答案 1 :(得分:0)

可能会有效

margin-left