Solved

Help with syntax error on SQL UNION command

Posted on 2004-09-22
4
2,845 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now