Same column name from different table
i have a sql query as like this : "SELECT * FROM TABLE1,TABLE2". i use oracle. both TABLE1 and TABLE2 have the same column named 'COLUMN1'. while i get rows how i know the value of COLUMN1 from which table (TABLE1 or TABLE2).
sample code snippet is above. do u help me!
while (rs.next())
value1 = rs.getString("COLUMN1");
// is value1's value from table1 or table2. how do i know this?
// i try value1 = rs.getString("TABLE1.COLUMN1"); but it doesn't work :(
....
I case you don't know what an alias is, it would look something like this:
SELECT a.COLUMN1 as FirstColumn1, b.COLUMN1 as SecondColumn1 FROM FirstTable a, SecondTable b
Notice that in the FROM clause we've appended a short name for each table. You're not limited to one character, but I try to keep it simple. Now we can refer to the tables as a and b.
Because I did that I have to refer to any ambiguous columns (although it's good practice to refer to ALL columns) using the table name prefix and a period. This tells the driver which "COLUMN1" I want. Then we include as AS clause which allows us to tell the driver what we want that column name to be when it's returned to us. This is specially usefule when I have two columns in two separate tables with the same name (as you have here) or if I'm calculating data (i.e. (a.QTY * b.PRICE) as UnitPrice) that doesn't have a column name, so here I can give it one.
It's a little weird at first since you use the alias names in the select before you actually define them in the FROM clause, but you'll get use to it.
Now you retrieve FirstColumn1 and SecondColumn1 from your ResultSet, not Column1.
HTH.
Similar Messages
-
How to get only column names from different tables as single table columns
Hi All,
I have one requirement in which we want only column names from different tables.
for example :
I have three tables T1 ,T2, T3 having
col1 clo2 clo3 --> T1 ,
col3 col5 ,clo6 --> T2 ,
Clo6 col8 col9 --> T3
columns i want to get only all Column names from all table as single Resultset not any data from that how can i get that empty resultset
because this empty result i want to bind in datagridview(front end) as Empty resultset
Please tell me anyways to do this
Niraj SevalkarIf I understand you want an empty result set, just with metadata. SET FMTONLY do the trick:
SET FMTONLY ON
SELECT Col1, Col2, Col3, ....., Coln
FROM
T1 CROSS JOIN T2 CROSS JOIN T3
SET FMTONLY OFF
Another alternative is to include an imposible contition
SELECT Col1, Col2, Col3, ....., Coln
FROM
T1 CROSS JOIN T2 CROSS JOIN T3
WHERE 1 = 0
If you are using a SqlDataAdapter in your client application. You can use the FillSchema method. the select command may be any select statement that returns the columns you want. Under the covers FillSchema will call SET FMTONLY ON.
If you are using SqlCommand.ExecuteReader you can pass SchemaOnly to CommandBehavior argument. SET FMTONLY ON is called under the covers. Again the select command may be any select statement that returns the columns you want.
"No darás tropezón ni desatino que no te haga adelantar camino" Bernardo Balbuena -
How To Create Table View With Same Column name But Different Table?
Hi All,
I have the problem to create a tableview with same column name but in different table.
The Table that i have:-
Table - PAC051MPROFORMA
Column - mrn,visitid
Table - PAC051TPROFORMA
Column - mrn,visitid
Table - PAC052MTRANSBILL
Column - mrn,visitid
Then i want to create a table view to view that table. This is my SQL
CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
As Select PAC051MPROFORMA.mrn,PAC051MPROFORMA.visitid,PAC051TPROFORMA.mrn,PAC051TPROFORMA.visitid,PAC052MTRANSBILL.mrn,PAC052MTRANSBILL.visitid
where
*(a.PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)*
and
*(a.PAC051TPROFORMA.mrn=PAC052TRANSBILL.mrn)*
That SQL Return this error = ORA-00957: duplicate column name
Then I modify that SQL to
CREATE VIEW pacviewproforma (mrn,visitid)
As Select PAC051MPROFORMA.mrn,PAC051MPROFORMA.visitid,PAC051TPROFORMA.mrn,PAC051TPROFORMA.visitid,PAC052MTRANSBILL.mrn,PAC052MTRANSBILL.visitid
where
*(a.PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)*
and
*(a.PAC051TPROFORMA.mrn=PAC052TRANSBILL.mrn)*
This time this error return = ORA-01730: invalid number of column names specified
What should i do?
Thanks...Hi,
SQL> CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
2 As Select
3 PAC051MPROFORMA.mrn,
4 PAC051MPROFORMA.visitid,
5 PAC051TPROFORMA.mrn,
6 PAC051TPROFORMA.visitid,
7 PAC052MTRANSBILL.mrn,
8 PAC052MTRANSBILL.visitid
9 from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
10 where
11 (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
12 and
13 (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
ERROR at line 1:
ORA-00957: duplicate column namePlease give different names to each column.
Something like this..
SQL> CREATE OR REPLACE VIEW pacviewproforma (MPROFORMA_mrn,MPROFORMA_visitid,TPROFORMA_mrn,TPROFORMA
_visitid,MTRANSBILL_mrn,MTRANSBILL_visitid)
2 As Select
3 PAC051MPROFORMA.mrn,
4 PAC051MPROFORMA.visitid,
5 PAC051TPROFORMA.mrn,
6 PAC051TPROFORMA.visitid,
7 PAC052MTRANSBILL.mrn,
8 PAC052MTRANSBILL.visitid
9 from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
10 where
11 (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
12 and
13 (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
View created.
SQL> DESC pacviewproforma;
Name Null? Type
MPROFORMA_MRN NUMBER
MPROFORMA_VISITID NUMBER
TPROFORMA_MRN NUMBER
TPROFORMA_VISITID NUMBER
MTRANSBILL_MRN NUMBER
MTRANSBILL_VISITID NUMBER
ORA-01730: invalid number of column names specifiedThe list of column nmae you specified during the CREATE VIEW should match with the SELECT list of the view.
Twinkle -
Update SAME column name in two tables from ONE query
Dear All Seniors
Please tell me is it possible to update a same column name in two tables.
I have two tables in same schema
(1)table name
pem.igp_parent
column name
igp_no.
igp_type
(2)table name
pem.igp_child
column name
igp_no.
igp_type
i want to update igp_no column in one query please tell me how it would be possible.
thanks
yassenYou want to update the data from what to what? Where is the new data coming from?
If you are trying to put the same data in two different tables, that strongly implies that you have a normalization problem that needs to be addressed.
Why do you want a single query rather than updating each table in turn? Can you join the two target tables to produce a key-preserved view?
Justin -
How to Select data using same column name from 3 remote database
Hi,
Can anyone help me on how to get data with same column names from 3 remote database and a single alias.
Ex.
SELECT *
a.name, b.status, SUM(b.qty) qantity, MAX(b.date) date_as_of
FROM
*((table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3)a,*
*(table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3)b)*
WHERE b.dept = 'finance'
AND a.position = 'admin'
AND a.latest = 'Y' AND (b.status <> 'TRM') AND b.qty > 0;
GROUP BY a.name, b.status ;
NOTE: the bold statements is just an example of what I want to do but I always gets an error beacause of ambiguous columns.
Thanks in advnce. :)
Edited by: user12994685 on Jan 4, 2011 9:42 PMuser12994685 wrote:
Can anyone help me on how to get data with same column names from 3 remote database and a single alias.Invalid. This does not make sense and breaks all scope resolution rules. And whether this is in a single database, or uses tables across databases, is irrelevant.
Each object must be uniquely identified. So you cannot do this:
select * from (table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3) a3 objects cannot share the same alias. Example:
SQL> select * from (dual, dual) d;
select * from (dual, dual) d
ERROR at line 1:
ORA-00907: missing right parenthesisYou need to combine the objects - using a join or union or similar. So it will need to be done as follows:
SQL> select * from (select * from dual d1, dual d2) d;
select * from (select * from dual d1, dual d2) d
ERROR at line 1:
ORA-00918: column ambiguously definedHowever, we need to have unique column names in a SQL projection - so the join of the tables need to project a unique set of columns. Thus:
SQL> select * from (select d1.dummy as dummy1, d2.dummy as dummy2 from dual d1, dual d2) d;
DUM DUM
X X
SQL> I suggest that you look closely at what scope is and how it applies in the SQL language - and ignore whether the objects referenced are local or remote as it has no impact to fundamentals of scope resolution. -
Dynamic Column Names from one table and its corresponding values from another table
I have 2 tables. First tables gives the specification if a column is required or not. we have the 2nd table with the same column name where we provide the actual values.
I want to select all the required columns from the 1st table and retrieve the values for those from the 2nd table. Both this i want to achieve in a single select statement.This wil require a dynamic Query with a Pivot
DECLARE @ColsPivot as VARCHAR(MAX);
DECLARE @Query AS VARCHAR(MAX);
1. Retreive the ID for all required field
SET @ColsPivot = (SELECT STUFF((SELECT ',' + quotename(CAST([RequirementID] as varchar(3))) FROM [dbo].[Requirement] WHERE required=1 FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,''));
This will give you : [1],[2],[3],[8],[9],[14] for exemple.
2. Build your Query
SET @Query ='SELECT ClientID,'+@ColsPivot+''
FROM (
SELECT [ClientID],[RequirementID],[Value]
FROM dbo.RequirementValue
WHERE ClientID=@CliendID --Optional SP parameter
)src
PIVOT(
MAX(Value)
for [RequirementID] in ('+@ColsPivot+')
) p';
3. Exec(@Query); -
Column names from another table
Hi All,
I have a scenario where i need to get names of a column from another table
for eg,
Table EMP
EmpNo EmpName EmpContact EmpPhone
1 xyz [email protected] 345
2 abc [email protected] 897
3 ttp [email protected] 345
The column names of this table can be configurable from some other place and its value is stored in another table like
Table Config (2 Columns)
Column_Name Value
EmpName First name
EmpContact Email
EmpPhone Mobile
Now i want to fetch the values from Emp table but with column headers that are changed and have a value in Config table.
If a column name is not there in config table then the original column name should come.
As shown below
EmpNo First name Email Mobile
1 xyz [email protected] 345
2 abc [email protected] 897
3 ttp [email protected] 345
Another eg, If EmpName is not changed and entered in second table , then i want to have the same name as the original EMP table has as shown below.
EmpNo EmpName Email Mobile
1 xyz [email protected] 345
2 abc [email protected] 897
3 ttp [email protected] 345
In other words something like this,
select empno,
EmpName as (select value from config where column_name=EmpName),
EmpContact as (select value from config where column_name=Empcontact),
EmpPhone as (select value from config where column_name=EmpPhone)
From EMP
Can some one please help me in providing a solution for this.
Edited by: 941386 on May 30, 2013 6:20 AMUnfortunately, I think this is a job for dynamic sql ...
Build your "query" first:
(note this won't work "as is", fix the syntax - but you get the idea.)
lv_str := 'select empno,
EmpName as ' || (select value from config where column_name=EmpName) || ',
EmpContact as ' || (select value from config where column_name=Empcontact) || ',
EmpPhone as ' || (select value from config where column_name=EmpPhone) || '
From EMP;';
execute immediate lv_str;Not sure if there's a better way or not.
Only other way I can think of is to leverage the way UNION [ALL] works.
So the following query:
select a, b, c from dual
union all
select d, e, f from dual
/returns data in columns "named" : "a, b, c"
Effectively renaming columns d, e, f. You just need to turn your data on edge in that first query, then throw out the rows (I don't know how to get it to work, but perhaps somebody else does?)
[edit]
another thought is create a view over top of the table, query that view, then drop the view :P
that would work nicely - avoid the dynamic SQL. shrug
[edit]
Edited by: Greg.Spall on May 30, 2013 9:37 AM -
Finding a column name from all tables
i have 85 tables in my user, i forget a column name and its table name. how to find that particular column amongst all tables i have.
hi
You can use User_tab_columns or All_tab_columns
SQL>Select Table_Name,Column_Name From user_tab_columns
where lower(table_name) like '%emp%' or
lower(column_name) like '%dept%'
Khurram Siddiqui
[email protected]
Message was edited by:
pcbyte12 -
Finding same column name from multiple database tables
I am needing to find all tables in a database that contain the same column. ie: which tables in database A have col_a in it.
Is it possible to do a sql code to qry all of this?
ThanksNo, Ignacio did not, at least, not really. ;)
The usage of ALL_TAB_COLUMNS only has info for tables which the actual user has access to.
If you need to be sure that you search each and every table, you have to use DBA_TAB_COLUMNS (or the connected user must be a super hero...).
Just wanted to point that out.
Regards,
Guido -
Passing Parameters For Same Columns In 2 Different Tables
OBIEE 11g
I have 2 different dimension tables in my Subject Area.
Dim1 has following columns: Promotion Code, Promotion Description, Promotion Date
Fact1 has following columns: a_count, b_count, c_count
Dim2 has following columns: Promotion Code, Promotion Description, Promotion Date
Fact2 has following columns: a_count, b_count, c_count, d_count
Report 1 has following columns: Promotion Code, Promotion Description, Promotion Date, a_count, b_count, c_count (From Dim1 & Fact1)
Report 2 has following columns: Promotion Code, Promotion Description, Promotion Date, a_count, b_count, c_count, d_count (From Dim2 & Fact2)
I have tried to pass the parameters Promotion Code, Promotion Description, Promotion Date from Report 1 to Report 2 but this has not worked for me as Report 2 is not receiving the parameters. Is there a way I can do this. Thanks in advance.Thanks for your response Jay,
In Report 1 I have added prompts (Prompts tab) for Promotion Code, Promotion Description and Promotion Date. I have also set the following presentation variables in the prompts:
PromotionCode,
PromotionDescription
PromotionDate
In Report 2 I then add a filter for each of the same columns and match them to the presentation variables created in Report 1 as follows:
Promotion Code is equal to / is in @{PromotionCode}
Promotion Description is equal to / is in @{PromotionDescription}
Promotion Date is equal to / is in @{PromotionDate}
My expectation is that the values in the presentation variables will be passed from the Prompts in Report 1 to the filters in Report 2. This is not what is happening. Im not sure what I am doing wrong. -
Finding the column names from a table.
I am on 10g
I would like to find out the columns of a table where there are null columns in a table.....this table contains about 300 + columns where i do not want to put where condition for all the columns
is there a way i can write a sql to find?
for a given table or the results set that i need to get, i will have same results for all the rows, so it
cant be like col1 is null on row1, but col2 is not null on row2 ...they are all identical....
example table, but it has 300 + cols
F_IND H_IND P_IND DMA_IND
N N
N N
N N
N N Thanksselect count(col1), count(col2), count(col3), ...
from your table;
The results with 0s are null throughout the table (or the table has no rows). -
Same index name for different tables in different schema
Just a quick query
Can two tables present in different schema of same database has same index name ?
Will there be any problem?
ThanksAnd just a quick answer:
859486 wrote:
Just a quick query
Can two tables present in different schema of same database has same index name ?Yes.
>
Will there be any problem?No.
>
Thanks -
How to find same field name from all tables?
Dear All,
I want to find those tables that contain a field name, say COMPANY_CODE, from all user tables. How can I do that by writing SQL?
Many thanks,
AndrewSELECT table_name
FROM user_tab_columns
WHERE column_name = 'COMPANY_CODE'Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
JTable not seeing Column names from Abstract table model
Here is the code for the getColumnNames method
It is getting called and it is send the correct data However the Headings do not match the data.
public String getColumnName(int col){
String s_retval;
switch (m_iMode){
case PhoneTableModel.NORMALVIEW:
default:
s_retval= m_sHeadingsDefault[col];
break;
System.err.println("TableNames "+s_retval);
return s_retval;
}Why do you show the part of the code that works?!
Show how you create and display your table!
What is displayed in the headings then?
Which JDK on which platform? -
Sum of same fields in two different tables
Hi ,
I am facing a problem, hope anyone would help
I have two tables.. here is the sample data..
create table t1
(item_ID NUMBER(2),
book_ID NUMBER(2),
price NUMBER(2))
insert into t1 (item_ID, book_ID, price) values (1,1,60);
insert into t1 (item_ID, book_ID, price) values (1,2,70);
insert into t1 (item_ID, book_ID, price) values (1,3,80);
insert into t1 (item_ID, book_ID, price) values (2,1,10);
insert into t1 (item_ID, book_ID, price) values (2,1,20);
insert into t1 (item_ID, book_ID, price) values (1,3,40);
create table t2
(item_ID NUMBER(2),
book_ID NUMBER(2),
price NUMBER(2))
insert into t2 (item_ID, book_ID, price) values (1,1,89);
insert into t2 (item_ID, book_ID, price) values (1,2,29);
insert into t2 (item_ID, book_ID, price) values (1,3,99);
insert into t2 (item_ID, book_ID, price) values (2,1,55);
insert into t2 (item_ID, book_ID, price) values (2,1,90);
insert into t2 (item_ID, book_ID, price) values (1,3,10);
as you can see, both tables have the same column names with different data. what
I require is that i need to calculate the SUM of price grouped by book_id from
both the tables in a single SQL statement output.
so my required output is this:
BOOK_ID SUM(PRICE)
1 323
2 99
3 229
where the SUM(price) for each book_id is for all the books in both the tables
where book_id = 1 and then 2 in the second row and then 3 in the third row.
Would anyone please help, as I am unable to write the query for this.?
Thanks,
Sonali.<quote>I was just curious if we can do that by some other SQl query and using UNION ALL operator...</quote>
Of course there are other ways to do it ... but not necessarily better ... just to satisfy your curiosity:
flip@FLOP> select book_id,sum(price) from t1 group by book_id;
BOOK_ID SUM(PRICE)
1 5898240
2 4587520
3 7864320
4 2621440
Elapsed: 00:00:01.02
flip@FLOP> select book_id,sum(price) from t2 group by book_id;
BOOK_ID SUM(PRICE)
1 30670848
2 3801088
3 15597568
5 1310720
Elapsed: 00:00:02.06
flip@FLOP> SELECT book_id, sum(price)
2 FROM ( select * from t1
3 union all
4 select * from t2
5 )
6 GROUP BY book_id;
Â
BOOK_ID SUM(PRICE)
1 36569088
2 8388608
3 23461888
4 2621440
5 1310720
Â
Elapsed: 00:00:04.01
flip@FLOP>
flip@FLOP> with a1 as
2 (
3 select book_id, sum(price) p
4 from t1
5 group by book_id
6 )
7 ,a2 as
8 (
9 select book_id, sum(price) p
10 from t2
11 group by book_id
12 )
13 select nvl(a1.book_id,a2.book_id), nvl(a1.p,0)+nvl(a2.p,0) p
14 from a1 full outer join a2 on (a1.book_id = a2.book_id)
15 ;
Â
NVL(A1.BOOK_ID,A2.BOOK_ID) P
1 36569088
2 8388608
3 23461888
4 2621440
5 1310720
Â
Elapsed: 00:00:04.02
Maybe you are looking for
-
Hello! My App store and some other preinstalled apps wont open. Please help!
I've recently started to use my Macbook Pro, and some apps won't open. I've tried to update the software many times, but it still won't open. I really wan't to buy Lion from App store, but (once again) it won't open. I've also tried to use the Applic
-
Opening URL for report in a new window
I have a report with URLs to other reports, and also to dialogs in Dynamics CRM. The URLs all work perfectly, however, I want to open the reports the URLs point to (and the CRM dialogs) in a new window. I tried adding &rc:LinkTarget=_blank to the end
-
Values of F4 help are not returned.
Hi, I had a requirement to add a F4 help for batches in VL02n transaction. It is using a collective search help MCH1for the F4 help . I created 1 elemntary search help ,with a necessary view. I then added my elementary search help to the collective
-
How to create is Dialog or JDialog without close button
I want to add a dialog report the current step of work in the GUI and I don't want it to be closed manually. How can I get rid of the close button on the top right side of the dialog? Thanx for any help or clue given.
-
How can I stream Live video?
I'm working on a project where I take live video from an NTSC source. I'm looking for a simple program to import and allow me to take screen captures on the fly. I can edit it later. Can iMovie 09 do this...if so how?