user1425503 user1425503 - 4 days ago 7
SQL Question

group by with (-) operator in oracle query

I have a query like this below. There is (

-
) operator =
(AA.SOURCE_CREDIT - BB.SOURCE_DEBIT / 1.1)
. I want to group by BR.AREA_ID. Just BR.AREA_ID. But it gave me error:


ORA-00979: not a GROUP BY expression


And I have to add AA.SOURCE_CREDIT and BB.SOURCE_DEBIT in group by. But the result becoming not what I want. Just AREA_ID that I want.

SELECT 'SALES EU' AS DESCRIPTION,
(AA.SOURCE_CREDIT - BB.SOURCE_DEBIT / 1.1) AS amount,
BR.AREA_ID
FROM VI_REPORT_BK_PENJUALAN BK
JOIN
MST_BRANCH BR
ON BR.BRANCH_ID = BK.BRANCH_ID
LEFT OUTER JOIN
(SELECT JH.JOURNAL_NO, JD.SOURCE_CREDIT
FROM TRX_JOURNAL_GL_HEADER JH
JOIN
TRX_JOURNAL_GL_DETAIL JD
ON JH.JOURNAL_NO = jd.JOURNAL_NO
JOIN
TRX_DELIVERY_CONFIRMATION DC
ON DC.DELIVERY_NO = JH.DOCUMENT_NO
WHERE JD.SOURCE_TYPE = '40100000') AA
ON (AA.JOURNAL_NO = BK.JOURNAL_NO)
LEFT OUTER JOIN
(SELECT JH.JOURNAL_NO, JD.SOURCE_DEBIT
FROM TRX_JOURNAL_GL_HEADER JH
JOIN
TRX_JOURNAL_GL_DETAIL JD
ON JH.JOURNAL_NO = jd.JOURNAL_NO
JOIN
TRX_DELIVERY_CONFIRMATION DC
ON DC.DELIVERY_NO = JH.DOCUMENT_NO
WHERE JD.SOURCE_TYPE = '40502002') BB
ON (BB.JOURNAL_NO = BK.JOURNAL_NO)
GROUP BY BR.AREA_ID, AA.SOURCE_CREDIT, BB.SOURCE_DEBIT


I already try to make group by inside the subs-query. But still not work. I'm browsing also in google but I didn't find the case like my case.

am2 am2
Answer

tf there are more rows than one to each row, what do you expect the query gives back? Example:

AREA_ID | AA.SOURCE_CREDIT | BB.SOURCE_DEBIT
   1    |      100         |       50
   1    |       70         |       50

It you are sure there is only one row for each area id, so you could use SUM, MAX, AVG or anything else.

SELECT   'SALES EU' AS DESCRIPTION,
           (MAX(AA.SOURCE_CREDIT) - MAX(BB.SOURCE_DEBIT) / 1.1) AS amount,
           BR.AREA_ID
...

If there could be more results you should decide, what Aggregate function fits best.

Comments