Home » SQL & PL/SQL » SQL & PL/SQL » Create Sql Query (Oracle 11g,Win7 23 bit)
Create Sql Query [message #615524] Thu, 05 June 2014 03:33 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hello All,
I want to create one query with the help of three tables like ..

create table tab1 (Pid number ,value number);
create table tab2 (Eid number ,Pid number);
create table tab3 (Did number ,Eid number,l_data varchar2(50));

alter table "TAB2" add constraint FK1 foreign key("PID") references "TAB1"("PID");
alter table "TAB3" add constraint FK2 foreign key("EID") references "TAB2"("EID");

insert into tab1(pid,value) values (101,1);
insert into tab2(Eid,PID) values (201,101);
insert into tab3(DID,EID,L_DATA) values (501,201,'Active');
insert into tab3(DID,EID,L_DATA) values (502,201,'Inactive');

When i am going to select the data from tab1 then the "value" should be pull out from tab3 and "value" 1 stands for 'Active' (501)
and 2 stands for 'Inactive'(502).

so the final output like ..

PID | VALUE | EID
---------------------
101 | Active | 201


Thanks,
Xandot
Re: Create Sql Query [message #615526 is a reply to message #615524] Thu, 05 June 2014 03:41 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Looks like a simple JOIN ... I don't see anything special in this question, so - did you try to solve the problem? If not, you should. If you still can't make it work, post your own query and someone will assist.
Re: Create Sql Query [message #615529 is a reply to message #615524] Thu, 05 June 2014 03:46 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
1. Join the tables.
2. Filter predicate as L_DATA = 'Active'
3. Project the required columns
Re: Create Sql Query [message #615530 is a reply to message #615529] Thu, 05 June 2014 03:54 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Its simple but if the tab3 contains 100 rows like 501..600 then the user can select value between 0..99 so how can i manage the in the query because user can select any number in the value field between 0 to 99 ..
-----------MY Query ----------
select distinct A.PID,decode(a.value,1,'Active',0,'Inactive'),C.EID from tab1 a,tab2 b,tab3 c
where a.pid=b.pid
and b.eid=c.eid
----------------------
But how many times i can put decode condition here ...can i use row_number for 100 rows filtration..

Thanks

[Updated on: Thu, 05 June 2014 04:05]

Report message to a moderator

Re: Create Sql Query [message #615531 is a reply to message #615530] Thu, 05 June 2014 04:05 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Why do you need DECODE? (In)Active information is stored in TAB3.L_DATA column.
Re: Create Sql Query [message #615533 is a reply to message #615531] Thu, 05 June 2014 04:10 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Because of the "value" is 1 and 1 stands for "Active" that's why i am using decode ...
Re: Create Sql Query [message #615534 is a reply to message #615533] Thu, 05 June 2014 04:12 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Then what is the point of the l_data column? Why does it exist?
Re: Create Sql Query [message #615536 is a reply to message #615530] Thu, 05 June 2014 04:14 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
SQL> -- Create tables
SQL> CREATE TABLE tab1
  2    (
  3       pid   NUMBER PRIMARY KEY,
  4       value NUMBER
  5    );
Table created
SQL> CREATE TABLE tab2
  2    (
  3       eid NUMBER PRIMARY KEY,
  4       pid NUMBER
  5    );
Table created
SQL> CREATE TABLE tab3
  2    (
  3       did    NUMBER,
  4       eid    NUMBER,
  5       l_data VARCHAR2(50)
  6    );
Table created
SQL> --Insert values
SQL> INSERT INTO tab1
  2              (pid,
  3               value)
  4  VALUES      (101,
  5               1);
1 row inserted
SQL> INSERT INTO tab2
  2              (eid,
  3               pid)
  4  VALUES      (201,
  5               101);
1 row inserted
SQL> INSERT INTO tab3
  2              (did,
  3               eid,
  4               l_data)
  5  VALUES      (501,
  6               201,
  7               'Active');
1 row inserted
SQL> INSERT INTO tab3
  2              (did,
  3               eid,
  4               l_data)
  5  VALUES      (502,
  6               201,
  7               'Inactive');
1 row inserted
SQL> -- query
SQL> SELECT t1.pid, t3.l_data, t2.eid FROM
  2  tab1 t1, tab2 t2, tab3 t3
  3  WHERE t1.pid=t2.pid
  4  AND t2.eid=t3.eid
  5  AND t3.l_data='Active';
       PID L_DATA                                                    EID
---------- -------------------------------------------------- ----------
       101 Active                                                    201
Re: Create Sql Query [message #615537 is a reply to message #615536] Thu, 05 June 2014 04:19 Go to previous message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
cookiemonster you are right decode is not required ...I got the point ..

Thanks all for reply....


Previous Topic: How to insert value in a partition table if the partition is not defined.
Next Topic: Table count with rows and no rows
Goto Forum:
  


Current Time: Thu Apr 25 18:54:51 CDT 2024