0
我在ORACLE SQL中有一個查詢,只有當debet_amount或者credit_amount的SUM大於或等於1000時,我才需要返回結果v_no。ORACLE SQL-在WHERE子句中使用GROUP BY無效標識符
電流誤差
ORA-00904: "g"."COMPANY": invalid identifier
00904. 00000 - "%s: invalid identifier"
當前查詢
SELECT DISTINCT
g.company,
g.v_type,
g.v_no,
g.v_date,
g.a_year,
g.a_period,
g.entry_date,
g.correction,
CASE WHEN g.mcompany IS NULL THEN 'FALSE' ELSE g.mcompany END AS mcompany,
g.debet_amount,
g.credit_amount,
g.amount,
v.userid
FROM genrow g,
(SELECT v_no, SUM(debet_amount) AS debcomp FROM genrow GROUP BY v_no) subdeb,
(SELECT v_no, SUM(credit_amount) AS credcomp FROM genrow GROUP BY v_no) subcred
INNER JOIN genv v ON g.v_no = v.v_no AND g.company = v.company
WHERE g.v_no = subdeb.v_no
AND g.v_no = subcred.v_no
AND (subdeb.debcomp >= 1000 or subcred.credcomp >= 1000)
AND g.v_type IN ('M','Q','E')
實施例的預期輸出
company v_type v_no v_date a_year a_period entry_date correction mcompnay debet_amount credit_amount amount userid
02 EXT 2000 25-JAN-14 2014 1 28-JAN-14 N FALSE 600 600 USER13
02 EXT 2000 25-JAN-14 2014 1 28-JAN-14 N FALSE 200 200 USER16
02 EXT 2000 25-JAN-14 2014 1 28-JAN-14 N FALSE 300 300 USER12
02 EXT 4000 25-JAN-14 2014 1 28-JAN-14 N FALSE 500 500 USER19
02 M 4000 29-MAR-14 2014 3 07-APR-14 N FALSE 200 200 USER12
02 EXT 4000 25-JAN-14 2014 1 28-JAN-14 N FALSE 120 120 USER18
02 EXT 4000 25-JAN-14 2014 1 28-JAN-14 N FALSE 700 700 USER14
02 EXT 6000 25-JAN-14 2014 1 28-JAN-14 N FALSE 1100 1100 USER19
看起來像 'GENROW' 表不包含 'mcompany' 列。可以? – 2014-12-03 10:32:42
是的,它包含它 – Matt 2014-12-03 10:36:09
'AND(subdeb.debcomp> = 1000或subcred.credcomp> = 1000)'可以作爲'HAVING'子句的一部分移入子查詢中,這可以提高性能它更容易閱讀)。 – 2014-12-03 11:53:00