AllExperts > Experts 
Search      

Oracle

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Oracle Answers
Question Library

Ask a question about Oracle
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Suchitra Joshi
Expertise
I can answer questions regarding SQL, PL/SQL, Procedures, Functions, Triggers, SQL Loader and some basic dba activities or performance tuning for Oracle 8i database. I cannot answer questions related to major dba activities, backup and recovery.

Experience
10+ years
D2K

Awards and Honors
OCP - SQL and PL/SQL
Brainbench certifications in Oracle Administration, Database, PL/SQL

 
   

You are here:  Experts > Computing/Technology > Oracle > Oracle > union syntax

Topic: Oracle



Expert: Suchitra Joshi
Date: 6/11/2008
Subject: union syntax

Question
how to fetch fields from three tables in a single statement
The Query(select a.*,b.*,s.cpp,s.windmill from anntmp a,anntmp1 b, servicemas s where         s.circlecode=c1.circlecode and s.serviceno=c1.serviceno and a.serviceno=b.serviceno and b.serviceno=s.serviceno and a.circlecode=b.circlecode and b.circlecode=s.circlecode  and a.ltorhtside=s.ltorhtside and LPAD(ltrim(rtrim(billmonth)),6,'0') =:bill.monyear) returns the error "alias name required to avoid dupilcate column names"  

Answer
Hi Savitha,

The error indicates that there are some duplicate column names in some of the 3 tables.

For eg.

tab1 table contains columns a1, a2, a3
tab2 table contains columns a1, a2, b1, b2
tab3 table contains columns a1, a2, b1, c1

Then the above error will occur since -
- columns a1 and a2 are present in all tables tab1, tab2 and tab3
- column b1 is present in both tables tab2 and tab3

So use the following query in your case (not sure what c1 is) -

select a.serviceno a_serviceno,
      a.circlecode a_circlecode,
      a.ltorhtside a_ltorhtside,
      b.serviceno b_serviceno,
      b.circlecode b_circlecode,
      s.ltorhtside s_ltorhtside,
      s.cpp        s_cpp,
      s.windmill   s_windmill
from anntmp a,anntmp1 b, servicemas s
where  s.circlecode=c1.circlecode
and    s.serviceno=c1.serviceno
and a.serviceno=b.serviceno
and b.serviceno=s.serviceno
and a.circlecode=b.circlecode
and b.circlecode=s.circlecode  
and a.ltorhtside=s.ltorhtside
and LPAD(ltrim(rtrim(billmonth)),6,'0') =:bill.monyear

Here a_serviceno, a_circlecode, a_ltorhtside, b_serviceno, b_circlecode, s_ltorhtside are all alias names. You will have to give all column names from all tables in the select list (instead of *). Alias names can be any names similar to any column names.

Hope this helps

Regards

Suchitra

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.