Oracle PL/SQL: Group By - Having 用法

下面是 Select 中, Group By - Having 的用法 :

語法, 如下 :
 語法
SELECT 一般欄位 ...
, 彙總欄位 ...
FROM ...
WHERE ...
GROUP BY 所有一般欄位 ...
[HAVING 彙總欄位的條件 ...];



範例 : 查詢本位幣科目餘額表
 範例
SELECT TO_CHAR(GPS.END_DATE, 'YYYY/MM')
, GCC.SEGMENT3 || '.' || GCC.SEGMENT4 || '.' || GCC.SEGMENT5 ACC_NO
, GB.CURRENCY_CODE CURRENCY

, SUM(GB.BEGIN_BALANCE_DR + GB.PERIOD_NET_DR) ACCOUNTED_DR
, SUM(GB.BEGIN_BALANCE_CR + GB.PERIOD_NET_CR) ACCOUNTED_CR
, SUM((GB.BEGIN_BALANCE_DR + GB.PERIOD_NET_DR) - (GB.BEGIN_BALANCE_CR + GB.PERIOD_NET_CR)) AMT

FROM GL_BALANCES GB
, GL_PERIOD_STATUSES GPS
, GL_CODE_COMBINATIONS GCC
WHERE GB.CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
AND GB.SET_OF_BOOKS_ID = GPS.SET_OF_BOOKS_ID
AND GB.PERIOD_NAME = GPS.PERIOD_NAME
AND GPS.APPLICATION_ID = 101
AND GB.TRANSLATED_FLAG IS NULL
AND GB.SET_OF_BOOKS_ID IN (567)
AND GB.PERIOD_NAME IN ('ADJ03-07')
GROUP BY GPS.END_DATE
, GCC.SEGMENT3 || '.' || GCC.SEGMENT4 || '.' || GCC.SEGMENT5
, GB.CURRENCY_CODE
HAVING SUM(GB.BEGIN_BALANCE_DR + GB.PERIOD_NET_DR) <> 0
OR SUM(GB.BEGIN_BALANCE_CR + GB.PERIOD_NET_CR) <> 0
;
Related Posts Plugin for WordPress, Blogger...