Solved

Help with syntax error on SQL UNION command

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now