Solved

Help with syntax error on SQL UNION command

Posted on 2004-09-22
4
3,010 Views
Last Modified: 2007-12-19
This SQL command:

SELECT
  IT_TYPE  type,
  IT_DBAC1 db_acct,
  IT_CRAC1 cr_acct,
  IT_AMNT1 total
FROM IVTR_AML
WHERE IT_AMNT1 > 0

UNION

SELECT
  IT_TYPE  type,
  IT_DBAC2 db_acct,
  IT_CRAC2 cr_acct,
  IT_AMNT2 total
FROM IVTR_AML
WHERE IT_AMNT2 > 0

GROUP BY type, db_acct, cr_acct;

EXIT;

Produces these errors:

SP2-0042: unknown command "UNION" - rest of line ignored.
SP2-0734: unknown command beginning "GROUP BY t..." - rest of line ignored.

Why?

I'm running the command in SQLPlus against an Oracle 8i database running on an IBM RS/6000 computer.
0
Comment
Question by:jrollins138
  • 2
  • 2
4 Comments
 

Expert Comment

by:CedD
ID: 12124096
Remove empty lines and it will be ok

SELECT
  IT_TYPE  type,
  IT_DBAC1 db_acct,
  IT_CRAC1 cr_acct,
  IT_AMNT1 total
FROM IVTR_AML
WHERE IT_AMNT1 > 0
UNION
SELECT
  IT_TYPE  type,
  IT_DBAC2 db_acct,
  IT_CRAC2 cr_acct,
  IT_AMNT2 total
FROM IVTR_AML
WHERE IT_AMNT2 > 0
GROUP BY type, db_acct, cr_acct;
0
 

Author Comment

by:jrollins138
ID: 12124268
Did that and now I'm getting this ('*' is under the 'c' in cr_acct):

GROUP BY type, db_acct, cr_acct
                                    *
ERROR at line 16:
ORA-00904: invalid column name

Any suggestions?
0
 

Accepted Solution

by:
CedD earned 300 total points
ID: 12124373
It depends on what you want to group by.

First, the correct query is much like :

select type, db_acct, cr_acct, total from (
SELECT
  IT_TYPE  type,
  IT_DBAC1 db_acct,
  IT_CRAC1 cr_acct,
  IT_AMNT1 total
FROM IVTR_AML
WHERE IT_AMNT1 > 0
UNION
SELECT
  IT_TYPE  type,
  IT_DBAC2 db_acct,
  IT_CRAC2 cr_acct,
  IT_AMNT2 total
FROM IVTR_AML
WHERE IT_AMNT2 > 0 )
GROUP BY type, db_acct, cr_acct, total;

For the group by, that depends on what you want to do after the union.
0
 

Author Comment

by:jrollins138
ID: 12126009
That nailed it. Here's the resulting query:

SELECT type, db_acct, cr_acct, SUM(total) FROM (
SELECT
  IT_TYPE  type,
  IT_DBAC1 db_acct,
  IT_CRAC1 cr_acct,
  IT_AMNT1 total
FROM IVTR_AML
WHERE IT_AMNT1 > 0
UNION
SELECT
  IT_TYPE  type,
  IT_DBAC2 db_acct,
  IT_CRAC2 cr_acct,
  IT_AMNT2 total
FROM IVTR_AML
WHERE IT_AMNT2 > 0)
GROUP BY type, db_acct, cr_acct;

What I've got is a record that has two sets of debit/credit/amount fields. What I want is a list of records summarized by type/debit account/credit account. The UNION aligns the second set of fields with the first and the SUM/GROUP BY handles the math. Records from the resulting query will show me the total amount associated with each type/debit/credit combination. Thanks for the help.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with duplicate records in Oracle query 16 52
sum of columns in a row in oracle 3 44
Oracle 12c Default Isolation Level 17 56
return value in based on value passed 6 38
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question