我有三个表,分别称为bus,bustype,depo。总线表包含总线名称和来自总线类型(bustypeid)和depo(depoid)的外键。 总线类型为SF,FP,LS等,并且depo表包含不同的depos。
我需要获取按depos分组的总线列表,然后再次按总线类型分组。
表-公交车
bustypeid busname
1 SF
2 FP
3 LS
表-总线类型
depoid deponame
7 depo1
4 depo2
表格-仓库
SELECT COUNT(busid) as availability,BS.bustype, B.depoid FROM `bus` as B inner join bustype as BS on B.`bustypeid`=BS.bustypeid GROUP BY BS.bustype
我将查询写成按bustypeid分组,即
availability bustype depoid
1 FP 7
1 LS 4
2 SF 7
现在我也需要再次按depo将其分组。 结果可能看起来像
SELECT COUNT(1) FROM
(SELECT /*+ FULL(T) PARALLEL(T)*/ T.ID
FROM MY_TABLE T
WHERE T.DATE BETWEEN
ADD_MONTHS(TO_DATE('01/12/2018', 'dd/mm/yyyy'), -6)
AND
LAST_DAY(TO_DATE('01/12/2018', 'dd/mm/yyyy'))
GROUP BY T.ID);
答案 0 :(得分:2)
只需将其添加到group by
子句中即可:
.... GROUP BY BS.bustype, depoid