Solved

Help with syntax error on SQL UNION command

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clone Oracle 12c Database 5 52
dates - loop 12 65
Need a replacement data type in Oracle 6 77
form builder not starting 3 34
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

786 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