Outer Join and joining several Tables
HelloOracle Database 10 g Express Edition Release 10.2.0.1.0 - product
I have three tables AddProject, AssociateProjectLead, AddAssociate. Now I'm generating a report who will join AddProject and AssociateProjectLead for a list of all the projects, even if it doenst have a project coordinator. I used the outer join for this. However, if I want the name of the project leader, I need to search for AddAssociate table. By joining this table also, the outer join is no longer valid. Please see the result below
CREATE TABLE "ADDPROJECT"
( "VERSIONNO" NUMBER(*,0),
"PROJID" VARCHAR2(20),
"PROJNAME" VARCHAR2(60),
"PROJSTARTDATE" DATE,
"PROJSTATUS" VARCHAR2(20),
"PROJENDDATE" DATE,
"PROJENDTYPE" VARCHAR2(20),
"PROJENDREASON" VARCHAR2(1000),
"UCPROJECTMANAGER" VARCHAR2(20),
"FROMDATE" DATE,
"TODATE" DATE,
"SRCHFIELD" VARCHAR2(20),
"OPERATOR" VARCHAR2(20),
"PARENTPROJID" VARCHAR2(20),
"PROJHIDDENDATE" VARCHAR2(20),
CONSTRAINT "PK_B36" PRIMARY KEY ("PROJID", "PROJHIDDENDATE") ENABLE
)
CREATE TABLE "ADDASSOCIATE"
( "VERSIONNO" NUMBER(*,0),
"ASSOCIATEID" NUMBER(9,0) NOT NULL ENABLE,
"ASSOCIATENAME" VARCHAR2(100) NOT NULL ENABLE,
"CTOJOINDATE" DATE,
"STATUS" VARCHAR2(20),
"ENDDATE" DATE,
"SRCHFIELD" VARCHAR2(20),
"OPERATOR" VARCHAR2(20),
"FROMDATE" DATE,
"TODATE" DATE,
CONSTRAINT "PK_B23" PRIMARY KEY ("ASSOCIATEID") ENABLE
)
CREATE TABLE "ASSOCIATEPROJECTLEAD"
( "VERSIONNO" NUMBER(*,0),
"PROJLEADASSOID" NUMBER(9,0),
"PROJID" VARCHAR2(20),
"ASSOCIATEID" NUMBER(9,0),
"PROJLEADSTARTDATE" DATE,
"STATUS" VARCHAR2(20),
"ASSOCPROJHIDDENDATE" VARCHAR2(20),
"PROJHIDDENDATE" VARCHAR2(20),
"ENDDATE" DATE,
CONSTRAINT "PK_B40" PRIMARY KEY ("ASSOCIATEID", "PROJID", "ASSOCPROJHIDDENDATE") ENABLE
)
elect ap.projid,apl.associateid
from addproject ap, associateprojectlead apl
where ap.projid = apl.projid(+)
and ap.projhiddendate = apl.projhiddendate(+)
PROJID ASSOCIATEID
Proj08 75825
Proj09 75825
Proj10 75825
Proj11 75825
Proj12 259811
Proj01 103035
Proj02 103035
Proj03 320092
Proj04 320092
Proj05 120974
Proj06 367393
Proj07 117618
Proj07 224882
Proj07 246652
prj001 -
prj001 -
16 rows returned in 0.00 seconds
select ap.projid,apl.associateid,aa.associatename
from addproject ap, associateprojectlead apl,addassociate aa
where ap.projid = apl.projid(+)
and ap.projhiddendate = apl.projhiddendate(+)
and apl.associateid = aa.associateid
PROJID ASSOCIATEID ASSOCIATENAME
Proj11 75825 Amarendra Kumar Singh
Proj10 75825 Amarendra Kumar Singh
Proj09 75825 Amarendra Kumar Singh
Proj08 75825 Amarendra Kumar Singh
Proj02 103035 Rajesh Jayaprakash
Proj01 103035 Rajesh Jayaprakash
Proj07 117618 Chetan Malhotra
Proj05 120974 Perumal Rajaram
Proj07 224882 Dilshad Ahmad
Proj07 246652 Shankar Kausley
Proj12 259811 Arunchandar Arun Vasan
Proj04 320092 Venkatesh Sarangan
Proj03 320092 Venkatesh Sarangan
Proj06 367393 Venkata Ramakrishna P
14 rows returned in 0.00 seconds
How can I select all the values in table AddProject?Published by: Pramukh on August 23, 2012 12:18
Hello
I could get the result with a bit of modification
select ap.projid,
apl.associateid,
aa.associatename
from addproject ap
left outer join
associateprojectlead apl
on ap.projid = apl.projid
and ap.projhiddendate = apl.projhiddendate
left outer join
addassociate aa
on apl.associateid = aa.associateid
As a follow-up, I have a request more. In the report form, the user can select a particular project ID and the report should be generated as a result. For example;-he wants to see the results of the only "Proj08". I get the output as below, while the result should display only the details of 'Proj08 '.
select ap.projid,
apl.associateid,
aa.associatename
from addproject ap
left outer join
associateprojectlead apl
on ap.projid = apl.projid
and ap.projhiddendate = apl.projhiddendate
and ap.projID = 'Proj08'
left outer join
addassociate aa
on apl.associateid = aa.associateid
ORDER BY ap.projID
PROJID ASSOCIATEID ASSOCIATENAME
Proj01 - -
Proj02 - -
Proj03 - -
Proj04 - -
Proj05 - -
Proj06 - -
Proj07 - -
Proj08 75825 Amarendra Kumar Singh
Proj09 - -
Proj10 - -
Proj11 - -
Proj12 - -
prj001 - -
prj001 - -
Tags: Database
Similar Questions
-
Please see the attached link for table structures and data of the sample.
There will be several lines in the table of SALES with different SALETYPEs. There may be several lines in the table RETURNS, corresponding to some of the points of SALE. RETURNS the table has a foreign key to the SALE. The report that I am trying to generate with a select sql statement must include a line for each type of sale (from table SALETYPE) and the sum of sale amounts corresponding to this type of sale of the SALES table. The table may not return the entries for all types of sales and the amount of Scripture must be reported as zero. The SQL I use is below, but does not work. I hope that I have explained this correctly. Please see the doc attached for typical data that I work with. Can someone please?
https://spreadsheets.Google.com/pub?key=0AnDmnUJXOChwdGF2ZjdjdHFtMGF1MkJ3MWtjQ2JfUmc & hl = in & Single = true & gid = 0 & output = html
Select SUM (s.amount) as sum_amount, t.sale_type from SALES s, SALETYPE t, r RETURNS
where s.id = r.id
and s.sale_type_id = t.id
T.sale_type group;
Published by: 801941 on October 12, 2010 15:02Hello - the second displays almost had. what you want to do is joining several tables at once - impossible with the syntax of the oracle, but possible if you use ANSI SQL.
Wrote file afiedt.buf 1 WITH s AS 2 (SELECT 1 id, 1 sale_type_id, 10 amount FROM dual 3 UNION ALL 4 SELECT 2, 1, 10 FROM dual 5 UNION ALL 6 SELECT 3, 2, 10 FROM dual 7 UNION ALL 8 SELECT 4, 2, 10 FROM dual 9 UNION ALL 10 SELECT 5, 3, 10 FROM dual 11 UNION ALL 12 SELECT 6, 4, 10 FROM dual), 13 r AS 14 (SELECT 2 sale_id FROM dual 15 UNION ALL 16 SELECT 3 FROM dual 17 UNION ALL 18 SELECT 4 FROM dual), 19 t AS 20 (SELECT 1 sale_type_id, 100 sale_type FROM dual 21 UNION ALL 22 SELECT 2, 200 FROM dual 23 UNION ALL 24 SELECT 3, 300 FROM dual 25 UNION ALL 26 SELECT 4, 400 FROM dual) 27 SELECT nvl(SUM(s.amount),0) total_amount, t.sale_type 28 FROM t 29 join r on (1=1) 30 left outer join s on (s.id = r.sale_id and s.sale_type_id = t.sale_type_id) 31 --WHERE s.id(+) = r.sale_id 32 -- AND s.sale_type_id(+) = t.sale_type_id 33 GROUP BY t.sale_type 34* order by t.sale_type SQL> / TOTAL_AMOUNT SALE_TYPE ------------ ---------- 10 100 20 200 0 300 0 400 4 rows selected.
The two commented lines are what you are trying to achieve using the syntax of the oracle, but which will give you an error.
-
Hello
We are working on a data warehousing project and wonder how do to join several tables that each are versioned separately (type SCD 2 with a valid and valid to date).
Because for example, we get our client from a single source of information (id customer, name, etc.) and the information on the rate of customer from another source. The sources are different, we have the separate tables for them and each of them gets versioned independently.
Here's my customer table (with its own valid and valuable to the columns).
ID Name of the customer Valid from Valid until the 1 CitiBank 1 JANUARY 14 JANUARY 1, 15 1 New CitiBank 2 JANUARY 15 FEBRUARY 1, 15 1 Latest CitiBank 2 FEBRUARY 15 APRIL 1, 15 And similarly the Client side ID and rating information.
ID Note Valid from Valid until the 1 Platinum 1 JANUARY 14 FEBRUARY 1, 14 1 Premium FEBRUARY 1, 14 1ST MARCH 15 I want to merge the two tables above and present information at a glance. I have some difficulty to determine validates the valid columns.
ID Name of the customer Note Valid from (Calculated) Valid until the (calculated) 1 CitiBank Platinum 1 JANUARY 14 FEBRUARY 1, 14 1 CitiBank Premium FEBRUARY 1, 14 JANUARY 1, 15 1 New CitiBank Premium 2 JANUARY 15 FEBRUARY 1, 15 1 Latest CitiBank Premium 2 FEBRUARY 15 1ST MARCH 15 And it's the query I used to get the above result:
SELECT client. id ,
customer . name ,
CRM . level ,
Greatest (client. vld_fm , crm. vld_fm ),
Least (client. vld_to , crm. vld_to )
DE client client,
client_rating crm
OÙ client. id = crm. id
AND ( client. vld_fm <= crm. vld_fm
AND client. vld_fm <= crm. vld_to
AND client. vld_fm >= crm. vld_fm
AND client. vld_fm >= crm. vld_to )
OR ( client. vld_fm BETWEEN crm. vld_fm AND crm. vld_to )
OR ( client. vld_to BETWEEN crm. vld_fm AND crm. vld_to );
The problem is we have several data sources (and each with its own versions) and joins become so very very complex. Is there a better way to write the query?Or maybe a better way to design our tables?
Thanks for your help.
Anand
Hello
you only need ranges that overlap to join.
Re: How do to sql query in a loop
Is a simpler way to test if the x_start to x_stop range comes into conflict with the range of y_start to y_stop
WHERE x_start <= y_stop AND y_start <= x_stop
In other words, two overlapping if and only if everyone will start before the other ends one. If this is not obvious (and it was certainly not clear to me when I heard it), then look at it this way: two ranges are not overlapping if and only if one of them starts after the end of the other.
Concerning
Marcus
-
joining several tables to generate data
with itemlist as
(select 1 cid, listname 'test', 'r' flg Union double all the)
Select 2 cid, listname 'test2', ' not flg double
),
category 1)
Select 1 cid, 122 k' sub '123' catcode, of any double Union
Select 1 cid, '234i' catcode, sub '124' all Union double
Select 1 cid, 238 k' void '124' catcode, double
)
void)
Select 1 cid, sub '123' of any union double
Select 1 cid, sub ' 124' double
)
I want to write a static query that gathers all the tables and produces the following output assuming that
CID is you pass as a parameter
CID listname flg topic catcode
1 123 122 k r test
1 test of r 234i 124
1 124 238 k r test
Suppose the data as changes to follow and even cid 1 is to pass as a parameter:
with itemlist as
(select 1 cid, listname 'test', 'r' flg Union double all the)
Select 2 cid, listname 'test2', ' not flg double
),
category 1)
Select 1 cid, 122 k' sub '123' catcode, of any double Union
Select 1 cid, '234i' catcode, sub '124' all Union double
Select 1 cid, 238 k' void '124' catcode, double
)
void)
Select 2 cid, sub '123' of any union double
Select 2 cid, sub '124' double
)
then the output should be
CID listname flg topic catcode
1 r test 123
1 test of r 124
1 test of r 124
for a final example, suppose the data now as to user 1 as cid
with itemlist as
(select 1 cid, listname 'test', 'r' flg Union double all the)
Select 2 cid, listname 'test2', ' not flg double
),
category 1)
Select 2 cid, 122 k' sub '123' catcode, of any double Union
Select 2 cid, catcode '234i', '124' void in any union double
Select 2 cid, 238 k' void '124' catcode, double
)
void)
Select 1 cid, sub '123' of any union double
Select 2 cid, sub '124' double
)
output must be
CID listname flg topic catcode
1 test r 123
Basically, the user will pass a cid, in this case 1. I'll read the itemlist where cid = 1 and I want to join all the other tables
to itemlist in such a way that I can generate extra lines. If no entry is found for cid 1, for example, in category 1 or auxiliary table
then the beloging column of this table must be null in the result
can someone help me write a query to produce the output above
Hello
elmasduro wrote:
...
Here is an explanation of the output. a user will have a front screen where it can enter itemlist info.
then in a submenu may enter a category or sub or both or none of them. the user can select multiple categories or sub
When the user presses the button Save, an entry will be made in the itemlist table, for example the cid = 1.
If he choose a category but without Subs, then will be entered into the table at say cid = 1
but no entries will be made for sub because he chose not to all values. If he chooses void instead of category values
then sub taable will fill and category is not. If he chooses the category and under, then both will be filled.what I want to do is to join the itemlist with table category and sub table and get the data to the Scriptures, this intervention of the user.
These entries not found will be the null value column in the result.
for example, the cid = 1 itemlist, if user enter category but not void, itemlist will be the join with the table category and sub
but since sub has no entry for the cid = 1, the column object will be empty in the output, but catcode will fillI'm so confused.
It is a query (that is, just a SELECT statement, which shows the data already present in the tables), or is this a DML statement, that will add data to the tables? If it's just a query, then do not say things like "entries will be taken" or "filled with tables.
Assuming you want a request maybe you want something like this:
WITH c_and_s AS
(
SELECT NVL (c.cid, s.cid) AS cid
NVL (c.sub, s.sub) AS topic
, NVL2 (s.cid, c.catcode, NULL) AS catcode
CATEGORY 1 c
FULL OUTER JOIN void s ON c.cid = s.cid
AND c.sub = s.sub
)
SELECT i.cid, i.listname, i.flg
cs.subject, cs.catcode
Itemlist I have
LEFT OUTER JOIN c_and_s ON cs.cid = i.cid cs
WHERE the i.cid (1) - you can do the cids 2 or several at the same time, if you want to
ORDER BY i.cid, cs.subject, cs.catcode
;
This example gets the output you asked data sample you posted.
-
Hello I am trying to improve my self, I am working on the user for example HR oracle tablespaces
HR diagram
http://img.Photobucket.com/albums/v346/satanix/hrschema.PNG
Select * from
employees a
job_history.employee_id = a.employee_id left outer join job_history
departments of outer join left on departments.department_id = a.department_id
outer join left on jobs.job_id jobs = a.job_id
There is no problem up to now, but when I add
locations of outer join left on locations.location_id = departments.department_id
country of outer join left on countries.country_id = locations.country_id
regions of outer join left on regions.region_id = countries.region_id
they came to the table with null values
Please help thanksSelect * from
employees a
job_history.employee_id = a.employee_id left outer join job_history
departments of outer join left on departments.department_id = a.department_id
outer join left on jobs.job_id jobs = a.job_id
locations of outer join left on locations.location_id = departments.location_id
country of outer join left on countries.country_id = locations.country_id
regions of outer join left on regions.region_id = countries.region_idProbably the above query might work
Please check the answer correct or useful, if it is -
How to join several tables source and do the research?
I have a requirement to load a target table by joining 4 source tables. Also, I do a search on a field of table to transform codes and check for NULL values. What will be the best approach for load table target?
Is it possible to do it in a single interface, or do I need to create multiple interfaces to achieve this?
My basic source and target are oracle, and I am planing to use incremental update Oracle merge.
Thank youYou are in the right direction by creating an interface for this transformation.
You will need to drag the source drop 4 tables + the lookup table in the Sources of Interface window and then make the appropriate joins.
Also, look for NULL values in the transformation. Depends on what you want to do with NULL values. If you want to ignore, use a filter.
If you want to make mistakes, use a constraint.
If you want to convert them, use NVLStart with Oracle Update incremental and once successful, use incremental update Oracle MERGE.
-
Retriving data by joining several tables and total general
Hello PL/SQL gurus and experts.
I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
I have table-
I have the following three tablesDROP TABLE Person_Tb; create table Person_Tb(Person_ID,Person_FName,Person_LName) as select 1001, 'Nikki','Carlos' from dual union all select 1002, 'Nina', 'Harrison' from DUAL union all select 1003, 'Mike', 'Carwely' from dual union all select 1004, 'Michael', 'Robinson' from DUAL union all select 1005, 'Hendi', 'Klum' from dual union all select 1006, 'Robert', 'Regan' from dual union all select 1007, 'Maria', 'Washington' from dual union all select 1008, 'Kistrien', 'Obama' from dual union all select 1009, 'Andrew', 'Regan' from dual union all select 1010, 'Emma', 'Thomsan' from dual union all select 1011, 'Andy', 'Grant' from dual union all select 1012, 'Brandy', 'Decosta' from dual;
DROP TABLE Teacher; create table Teacher(Teacher_ID,Fee_Date,Account_ID) as select 1001, '04/04/2013',3004 from dual union all select 1002, '04/06/2013',3006 from DUAL union all select 1003, '04/08/2013',3014 from dual union all select 1004, '04/08/2013',3017 from DUAL union all select 1005, '04/09/2013',3019 from dual union all select 1011, '04/12/2013',3025 from dual union all select 1012, '04/12/2013',3046 from DUAL;
I am able to recover the data individually Tracher and student as -.DROP TABLE Student; create table Student(Student_ID,Subaccount_ID) as select 1006,3004 from dual union all select 1007,3006 from DUAL union all select 1008,3004 from dual union all select 1009,3017 from DUAL union all select 1010,3017 from dual;
Select distinct pt.Person_LName || ',' || pt.Person_FName Trdr_NM from Student st, person_Tb pt where st.Student_id = pt.PERSON_ID
I need to get the records for all teachers who are associated with students-Select distinct pt.Person_LName || ',' || pt.Person_FName SubAct_Invst from Teacher tc, person_Tb pt where tc.teacher_id = pt.PERSON_ID
But how to retrieve the data above format, many want to help. Thanks in advance and I really appericate your time and efforts.SubAct_Invst Trdr_NM ---------------------------------------------------- Carlos,Nikki Regan,Robert Obama,Kistrien Harrison,Nina Washington,Maria Carwely,Mike Robinson,Michael Regan,Andrew Thomsan,Emma Klum,Hendi Grant,Andy Decosta,Brandy ------------------------------------------------------ Grand Total 7 5
Ideally, your condition must be handled by a front-end tool...
If you need specifically using SQL, you can check my previous post...
-
Joining several tables in Oracle DB
I have the following tables.
1 AddProject
-PROJID
-projName
2 AddLab
-Labrador
-teacher
3 ProjLabAssociation
-PROJID
-Labrador
4 AddResearchArea
-raID
-raName
5 ProjRAAssociation
-PROJID
-raID
AddProject is associated with tables-AddLab and AddResearchArea
ProjLabAssociation and ProjRAAssociation are the association of the tables that contain the primary key of the corresponding tables. The two fields in these 2 tables are primary keys.
If AddProject has 3 entrances - (Proj1, ProjectA) (Proj2 ProjectB), (Proj3, ProjectC)
AddLab has 2 entrances - (Lab1, Bangalore), (Lab2, Chennai)
AddResearchArea has 2 inputs - AM1 (Green Computing) (RA2, Cloud Computing)
ProjLabAssociation has 2 entrances - (Proj2 Lab1), (Proj3, Lab1)
ProjRAAssociation has 3 entries - (Proj2 RA1), (Proj3, AM1), (Proj3, RA2)
If I ask by AddLab given for (Lab1, Bangalore), I should get the following columns in the result table
--------------------------------------------------------------------------------------------------------------------------------
Table2ID | Table2Name | Table3ID | Table3Name | Table1ID | Table1Name
Lab1. Bangalore | RA1 | Green Computing | Proj2 | Project b
Lab1. Bangalore | RA1 | Green Computing | Proj3 | Project c
Lab1. Bangalore | RA2 | Cloud Computing | Proj3 | Project c
--------------------------------------------------------------------------------------------------------------------------------
I tried the following commands but I m getting the expected result
1.
A SQLException exception is thrown by saying - java.sql.SQLException: ORA-01417: a table can be external joined as another tableSELECT * FROM AddLab al, ProjLabAssociation pl, AddProject ap WHERE al.labID = pl.labID(+) and ap.projID = pl.projID(+);
--------------------------------------------------------------------------------------------------------------------------------
2.
I do not get results expcted hereSELECT * FROM AddLab,AddProject,AddResearchArea WHERE labID in (select al.labID from ProjLabAssociation pl,AddLab al where al.labID = pl.labID) AND projID in (select ap.projID from ProjLabAssociation pl,Addproject ap where ap.projID = pl.projID) AND themeID in (select ar.raID from ProjRAAssociation pr, AddResearchArea ar where ar.raID = pr.raID) AND projID in (select ap.projID from ProjRAAssociation pr,Addproject ap where ap.projID = pr.projID) ORDER BY labID;
--------------------------------------------------------------------------------------------------------------------------------
Oracle version: 9i 10g / 11g
Can anyone help me in this.
Published by: user9205634 on December 22, 2011 03:40Hello
Below the code gives the result
with table1 as ( select 'Proj1' as table1id,'ProjectA' as table1name from dual union all select 'Proj2' as table1id,'ProjectB' as table1name from dual union all select 'Proj3' as table1id,'ProjectC' as table1name from dual union all select 'Proj4' as table1id,'ProjectD' as table1name from dual ) ,table2 as ( select 'Lab1' as table2id,'Bangalore' as table2name from dual union all select 'Lab2' as table2id,'Chennai' as table2name from dual union all select 'Lab3' as table2id,'Delhi' as table2name from dual ) , table3 as ( select 'RA1' as table3id,'Green Computing' as table3name from dual union all select 'RA2' as table3id,'Cloud Computing' as table3name from dual ) ,table1table2 as ( select 'Proj2' as table1id,'Lab1' as table2id from dual union all select 'Proj3' as table1id,'Lab1' as table2id from dual union all select 'Proj3' as table1id,'Lab2' as table2id from dual union all select 'Proj4' as table1id,'Lab3' as table2id from dual ) ,table1table3 as ( select 'Proj2' as table1id,'RA1' as table3id from dual union all select 'Proj3' as table1id,'RA1' as table3id from dual union all select 'Proj3' as table1id,'RA2' as table3id from dual ) select t2.table2id,t2.table2name,t3.table3id,t3.table3name,t1.table1id,t1.table1name from table1 t1,table2 t2,table3 t3,table1table2 jt1t2,table1table3 jt1t3 where jt1t2.table2id=t2.table2id and t1.table1id=jt1t2.table1id and jt1t3.table1id(+)=jt1t2.table1id and t3.table3id(+)=jt1t3.table3id order by t2.table2name,t3.table3id;
TABLE2ID TABLE2NAME TABLE3ID TABLE3NAME TABLE1ID TABLE1NAME -------- ---------- -------- --------------- -------- ---------- Lab1 Bangalore RA1 Green Computing Proj2 ProjectB Lab1 Bangalore RA1 Green Computing Proj3 ProjectC Lab1 Bangalore RA2 Cloud Computing Proj3 ProjectC Lab2 Chennai RA1 Green Computing Proj3 ProjectC Lab2 Chennai RA2 Cloud Computing Proj3 ProjectC Lab3 Delhi Proj4 ProjectD 6 rows selected
Kind regards
Prabhu -
Problem with joining several tables
Hello PL/SQL gurus and experts.
I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
I followed two tables
Student-
Fees-drop table TT3; create table TT3(FeesCntlr,CommCntlr,LatePayCntlr,Name,Age) as select 1,11,21,'Mike',25 from dual union all select 2,12,22,'Clark',26 from dual union all select 1,11,21,'Mike',27 from dual union all select 4,17,27,'Ussan',28 from dual union all select 5,13,21,'Linda',29 from dual union all select 6,14,24,'Obrek',35 from dual union all select 7,15,25,'Batty',45 from dual union all select 8,16,26,'Nicky',38 from dual union all select 1,11,21,'Mike',25 from dual union all select 2,12,22,'Clark',26 from dual union all select 1,11,21,'Mike',27 from dual union all select 4,17,27,'Ussan',28 from dual union all select 5,13,21,'Linda',29 from dual union all select 6,14,24,'Obrek',35 from dual union all select 7,15,25,'Batty',45 from dual union all select 8,16,26,'Nicky',38 from dual ;
I am looking forward to the fees, commissions and surcharges output for each student and by using the following SQL code.drop table TT4; create table TT4(FeesCntlr,CommCntlr,LatePayCntlr,Fees,Commission,Latepayment) as select 1,11,21,25000,4456,223 from dual union all select 2,12,22,356000,3321,0 from dual union all select 1,11,21,456000,223,33 from dual union all select 4,17,27,32890,0,0 from dual union all select 5,13,21,0,778,223 from dual union all select 6,14,24,332567,222,0 from dual union all select 7,15,25,228990,22,0 from dual union all select 8,16,26,0,0,2234 from dual union all select 1,11,21,22345,0,0 from dual union all select 2,12,22,2222789,2,33 from dual union all select 1,11,21,334567,0,0 from dual union all select 4,17,27,9987250,2,33 from dual union all select 5,13,21,2227890,0,0 from dual union all select 6,14,24,2278231,0,3345 from dual union all select 7,15,25,223456,2,0 from dual union all select 8,16,26,223890,0,0 from dual ;
but it gives me not the correct result and showing a lot of entries and display only the "Expenses" column then only I'm looking for the output to be -select t3.Name,t3.age,t4.Fees from tt3 t3, tt4 t4 where t3.FeesCntlr=t4.FeesCntlr union select t3.Name,t3.age,t4.Commission from tt3 t3, tt4 t4 where t3.CommCntlr=t4.CommCntlr union select t3.Name,t3.age,t4.Latepayment from tt3 t3, tt4 t4 where t3.LatePayCntlr=t4.LatePayCntlr
Kindly help me with this. Thank you in advance for your time and effort.Name Age Fees Commission Laypayment Mike 25 837912 4679 256 Clark 26 2578789 3323 33 Ussan 36 10020140 2 33 Linda 38 2227890 778 223 Obrek 38 2610798 222 3345 Batty 32 452446 24 0 Nicky 23 223890 0 2234
Hello
I see your table TT3 has duplicated lines. A reason for this?
Rather than run a separate, I created it without duplicated lines:
create table TT3(FeesCntlr,CommCntlr,LatePayCntlr,Name,Age) as select 1,11,21,'Mike',25 from dual union all select 2,12,22,'Clark',26 from dual union all select 4,17,27,'Ussan',28 from dual union all select 5,13,21,'Linda',29 from dual union all select 6,14,24,'Obrek',35 from dual union all select 7,15,25,'Batty',45 from dual union all select 8,16,26,'Nicky',38 from dual; create table TT4(FeesCntlr,CommCntlr,LatePayCntlr,Fees,Commission,Latepayment) as select 1,11,21,25000,4456,223 from dual union all select 2,12,22,356000,3321,0 from dual union all select 1,11,21,456000,223,33 from dual union all select 4,17,27,32890,0,0 from dual union all select 5,13,21,0,778,223 from dual union all select 6,14,24,332567,222,0 from dual union all select 7,15,25,228990,22,0 from dual union all select 8,16,26,0,0,2234 from dual union all select 1,11,21,22345,0,0 from dual union all select 2,12,22,2222789,2,33 from dual union all select 1,11,21,334567,0,0 from dual union all select 4,17,27,9987250,2,33 from dual union all select 5,13,21,2227890,0,0 from dual union all select 6,14,24,2278231,0,3345 from dual union all select 7,15,25,223456,2,0 from dual union all select 8,16,26,223890,0,0 from dual ; SELECT t3.name, t3.age, SUM (t4.fees) fees, SUM (t4.commission) commission , SUM (t4.latepayment) latepayment FROM tt3 t3, tt4 t4 WHERE t3.feescntlr = t4.feescntlr AND t3.commcntlr = t4.commcntlr AND t3.latepaycntlr = t4.latepaycntlr GROUP BY t3.name, t3.age; Output: NAME AGE FEES COMMISSION LATEPAYMENT ----- ---------- ---------- ---------- ----------- Mike 25 837912 4679 256 Ussan 28 10020140 2 33 Linda 29 2227890 778 223 Clark 26 2578789 3323 33 Obrek 35 2610798 222 3345 Nicky 38 223890 0 2234 Batty 45 452446 24 0
Kind regards.
AlPublished by: Alberto Faenza on November 6, 2012 12:40
-
Join several tables of two different patterns - is it possible?
If this is the case, what are the measures?
THXThis is the case.
You must prefix the tables with the schemaname tables is in, or create a synonym for the table. In addition, you must grant select rights from of a the reference schema owner schema:
SELECT A.*,B.* FROM SCHEMA1.TEST_TABLE A, SCHEMA2.TEST_TABLE B WHERE somecondition
-
Outer join complete Multi Table
What is the syntax to make a full outer join on three tables. All the examples I can find use using the clause, but in my case, the column names are not the same in the three tablesHello
Use IT instead of USE.
Assuming that all the 3 tables will have a common value in a column (whatever it is called):
... FROM table_x x FULL OUTER JOIN table_y y ON y.idy = x.idx FULL OUTER JOIN table_z z ON z.idz = COALESCE (x.idx, y.idy)
When comes the time to join table_z, you won't know which of the previous tables match, so you must use COALESCE to try them all.
If you need to join several tables in this way, the expression of COALESCE for the nth table will have (n - 1) arguments.
-
Join the tables in the responses
I would like to know if it is possible to join the tables together as below in the answers?
Select a.Field1, b.Field1
FROM tableA an outer join left tableA b on
a.Field1 = b.FieldIf you want to make an INNER JOIN, and then type INNER JOIN instead of LEFT INNER JOIN.
If you want to left outer join and LEFT OUTER JOIN instead of INTERNAL LEFT JOIN type.
Nothing asked as a left inner join. That is why it gives error + [nQSError: 27002] near
+. Thank you
-
Using Left Outer Join with reference
I have three tables.
Table 1: BOOK_DETAILS
Fields: BOOK_ID, BOOK_NAME
Table 2: BOOK_ISSUE_RECORD
Fields: BOOK_ID, USER_NAME
Table 3: BOOK_AUTHOR
Fields: BOOK_ID, AUTHOR_NAME
I must link table 1 and table 2 with a left outer join, because even if the book is not the questions to anyone, his name should come.
I have once again display the name of the author of books for each book.
I am able to create a query with the left outer join between table 1 and table 2. However, I am not able to give a reference to Table 3.
Can someone help me with this please.
Concerning
Hawkerselect d.book_name, a.book_author, i.user_name from book_details d join book_author a on (d.book_id = a.book_id) left join book_issue_recors i on (d.book_id = i.book_id) /
SY.
-
using (+) or left outer join
Dear Expert;
I've been playing by using the two symbol... and realized that they do the same thing... Is it true...? or am I wrong.
Thank you.Hello
user13328581 wrote:
Dear Expert;I've been playing by using the two symbol... and realized that they do the same thing... Is it true...? or am I wrong.
They all have two outer joins. The + rating was the original way to do it in Oracle. LEFT, RIGHT, and FULL OUTER JOIN introduced in Oracle 9, but the old way is still supported.
There are some situations (such as an outer join complete and outer-join a table to two different tables) that are better with the ANSI syntax (it's LEFT OUTER JOIN). It is possible to get the same results using +, but it must be combersome and/or inefficient workarounds. I suggest that you use always LEFT OUTER JOIN (or FULL OUTER JOIN, or, on occasions RIGHT OUTER JOIN). I think that you will find it easier and less error-prone.
-
Create a trigger instead of update several tables in a view
Dear everybody
I am trying to create a trigger that updates instead of to day joined several tables in a view, but I can't get my trigger to work. The create view command was as follows:
The view above connects a polygon table to the table of function names which means that a polygon appears several times in a view, even though one version of the polygon exists in the base table. This means the direct update of view cannot take place, since 1 polygon can appear multiple times in a view. The two original base tables and their columns names are:CREATE OR REPLACE VIEW VIEW_MI_JOIN_GC AS SELECT MAP_INDEX.mi_prinx, index_type_id, original_map_publication_id, original_map_sheet_number_id, name_of_feature, geog_coordinates_id, GEOG_COORDINATES.mi_prinx AS "mi_prinx_polygon", GEOG_COORDINATES.geographical_coordinates, GEOG_COORDINATES.mapinfo_style_row FROM MAP_INDEX JOIN GEOG_COORDINATES ON geog_coordinates_id=GEOG_COORDINATES.mi_prinx;
Relax, I created was as follows:MAP_INDEX --------- MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE MAPINFO_STYLE_ROW GEOGRAPHICAL_COORDINATES GEOG_COORDINATES_ID GEOG_COORDINATES ---------------- MI_PRINX GEOGRAPHICAL_COORDINATES MAPINFO_STYLE_ROW
The idea is that when I draw a new polygon in MapInfo and assign him a revised number geog_coordinates_id and the number of mi_prinx_polygon, which are the same, once I have save the view as then it updates the underlying tables. Geographical_coordinates and mapinfo_style_row of map_index table columns are columns with ancient polygon data which while not having currently new data inserted into them, are required for the previous data they contain. These data are currently being added to the geog_coordinates table with other scripts. The idea is that all the data is then read using a view and updates made to the view, triggering instead of relaxing, so data are not duplicated but still visible as if it were.CREATE OR REPLACE TRIGGER TRIG_VIEW_MI_JOIN_GC INSTEAD OF UPDATE ON VIEW_MI_JOIN_GC REFERENCING NEW AS NEW FOR EACH ROW BEGIN UPDATE MAP_INDEX SET mi_prinx = :NEW.mi_prinx, index_type_id = :NEW.index_type_id, original_map_publication_id = :NEW.original_map_publication_id, original_map_sheet_number_id = :NEW.original_map_sheet_number_id, name_of_feature = :NEW.name_of_feature, mapinfo_style_row = :NEW.mapinfo_style_row, geographical_coordinates = :NEW.geographical_coordinates, geog_coordinates_id = :NEW.geog_coordinates_id WHERE geog_coordinates_id = :OLD.geog_coordinates_id; UPDATE GEOG_COORDINATES SET mi_prinx = :NEW.mi_prinx, geographical_coordinates = :NEW.geographical_coordinates, mapinfo_style_row = :NEW.mapinfo_style_row WHERE mi_prinx = :OLD.mi_prinx; END; /
When I created first the relaxation above in SQLdeveloper it seems to run constantly. Then my computer crashed, not related to this work, and I lost session because I did not commit it. I was not ready to commit it because I believe that something is wrong.
I am in the trigger syntax correctly and I go about it in the right way? I want to only update the rows that have changed, that's why I was using: NEWS and: OLD.
Kind regards
Tim
Published by: user467357 on November 18, 2008 18:07
I modified my script a little because there were a few errors. for example. start and old as old and view name typoSomething like this->
satyaki> satyaki>select * from v$version; BANNER ---------------------------------------------------------------- Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod PL/SQL Release 10.2.0.3.0 - Production CORE 10.2.0.3.0 Production TNS for 32-bit Windows: Version 10.2.0.3.0 - Production NLSRTL Version 10.2.0.3.0 - Production Elapsed: 00:00:01.78 satyaki> satyaki> satyaki>create table MAP_INDEX 2 ( 3 mi_prinx NUMBER(10) not null, 4 index_type_id NUMBER(6) not null, 5 original_map_publication_id NUMBER(6) not null, 6 original_map_sheet_number_id NUMBER(6) not null, 7 name_of_feature VARCHAR2(80) not null, 8 mapinfo_style_row VARCHAR2(80), 9 geographical_coordinates SDO_GEOMETRY, 10 geog_coordinates_id NUMBER(10), 11 constraints pk_mi_prinx primary key(mi_prinx) 12 ); Table created. Elapsed: 00:00:04.39 satyaki> satyaki>create table GEOG_COORINDATES 2 ( 3 mi_prinx NUMBER(10) not null, 4 geographical_coordinates SDO_GEOMETRY, 5 mapinfo_style_row VARCHAR2(80), 6 constraints pk_mi_prinx_n primary key(mi_prinx) 7 ); Table created. Elapsed: 00:00:00.30 satyaki> satyaki> satyaki>CREATE OR REPLACE VIEW VIEW_MI_JOIN_GC 2 AS 3 SELECT MAP_INDEX.mi_prinx, 4 MAP_INDEX.index_type_id, 5 MAP_INDEX.original_map_publication_id, 6 MAP_INDEX.original_map_sheet_number_id, 7 MAP_INDEX.name_of_feature, 8 MAP_INDEX.geog_coordinates_id, 9 GEOG_COORINDATES.mi_prinx AS "mi_prinx_polygon", 10 GEOG_COORINDATES.geographical_coordinates, 11 GEOG_COORINDATES.mapinfo_style_row 12 FROM MAP_INDEX , GEOG_COORINDATES 13 WHERE MAP_INDEX.geog_coordinates_id=GEOG_COORINDATES.mi_prinx; View created. Elapsed: 00:00:00.32 satyaki> satyaki> satyaki>insert into MAP_INDEX values( 2 &mi_prinx, 3 &index_type_id, 4 &original_map_publication_id, 5 &original_map_sheet_number_id, 6 '&name_of_feature', 7 '&mapinfo_style_row', 8 null, 9 &geog_coordinates_id); Enter value for mi_prinx: 1 old 2: &mi_prinx, new 2: 1, Enter value for index_type_id: 44 old 3: &index_type_id, new 3: 44, Enter value for original_map_publication_id: 5678 old 4: &original_map_publication_id, new 4: 5678, Enter value for original_map_sheet_number_id: 356 old 5: &original_map_sheet_number_id, new 5: 356, Enter value for name_of_feature: AA old 6: '&name_of_feature', new 6: 'AA', Enter value for mapinfo_style_row: GG old 7: '&mapinfo_style_row', new 7: 'GG', Enter value for geog_coordinates_id: 9 old 9: &geog_coordinates_id) new 9: 9) 1 row created. Elapsed: 00:00:00.16 satyaki>/ Enter value for mi_prinx: 2 old 2: &mi_prinx, new 2: 2, Enter value for index_type_id: 55 old 3: &index_type_id, new 3: 55, Enter value for original_map_publication_id: 6789 old 4: &original_map_publication_id, new 4: 6789, Enter value for original_map_sheet_number_id: 357 old 5: &original_map_sheet_number_id, new 5: 357, Enter value for name_of_feature: BB old 6: '&name_of_feature', new 6: 'BB', Enter value for mapinfo_style_row: 10 old 7: '&mapinfo_style_row', new 7: '10', Enter value for geog_coordinates_id: 8 old 9: &geog_coordinates_id) new 9: 8) 1 row created. Elapsed: 00:00:00.04 satyaki> satyaki>commit; Commit complete. Elapsed: 00:00:00.03 satyaki> satyaki> satyaki>insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row'); Enter value for mi_prinx: 9 Enter value for mapinfo_style_row: FFG old 1: insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row') new 1: insert into GEOG_COORINDATES values(9,null,'FFG') 1 row created. Elapsed: 00:00:00.07 satyaki>/ Enter value for mi_prinx: 8 Enter value for mapinfo_style_row: GGT old 1: insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row') new 1: insert into GEOG_COORINDATES values(8,null,'GGT') 1 row created. Elapsed: 00:00:00.05 satyaki> satyaki>commit; Commit complete. Elapsed: 00:00:00.02 satyaki> satyaki>select * from VIEW_MI_JOIN_GC; MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES) ---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- --------------------------------------------------------------------------------------------------------------- 1 44 5678 356 AA 9 9 2 55 6789 357 BB 8 8 Elapsed: 00:00:00.09 satyaki> satyaki> satyaki>CREATE OR REPLACE TRIGGER TRIG_VIEW_MI_JOIN_GC 2 INSTEAD OF UPDATE ON VIEW_MI_JOIN_GC 3 FOR EACH ROW 4 DECLARE 5 m_info_svw varchar2(80); 6 BEGIN 7 m_info_svw := :NEW.mapinfo_style_row; 8 9 UPDATE MAP_INDEX 10 SET mi_prinx = :NEW.mi_prinx, 11 index_type_id = :NEW.index_type_id, 12 original_map_publication_id = :NEW.original_map_publication_id, 13 original_map_sheet_number_id = :NEW.original_map_sheet_number_id, 14 name_of_feature = :NEW.name_of_feature, 15 mapinfo_style_row = m_info_svw, 16 geographical_coordinates = :NEW.geographical_coordinates, 17 geog_coordinates_id = :NEW.geog_coordinates_id 18 WHERE geog_coordinates_id = :OLD.geog_coordinates_id; 19 UPDATE GEOG_COORINDATES 20 SET mi_prinx = :NEW.geog_coordinates_id, 21 geographical_coordinates = :NEW.geographical_coordinates, 22 mapinfo_style_row = m_info_svw 23 WHERE mi_prinx = :OLD.geog_coordinates_id; 24 END; 25 / Trigger created. Elapsed: 00:00:00.20 satyaki> satyaki>select * from VIEW_MI_JOIN_GC; MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES) ---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- --------------------------------------------------------------------------------------------------------------- 1 44 5678 356 CC 9 9 2 55 6789 357 BB 8 8 Elapsed: 00:00:00.09 satyaki> satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES; MI_PRINX MAPINFO_STYLE_ROW ---------- -------------------------------------------------------------------------------- 9 FFG 8 GGT Elapsed: 00:00:00.07 satyaki>select * from MAP_INDEX; MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE MAPINFO_STYLE_ROW GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), S ---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ------------------------------------------------------------------- 1 44 5678 356 CC HHH 2 55 6789 357 BB HHH Elapsed: 00:00:00.12 satyaki> satyaki>update VIEW_MI_JOIN_GC 2 set name_of_feature = 'DD' 3 where mi_prinx = 1; 1 row updated. Elapsed: 00:00:00.05 satyaki> satyaki>select * from VIEW_MI_JOIN_GC; MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES) ---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- --------------------------------------------------------------------------------------------------------------- 1 44 5678 356 DD 9 9 2 55 6789 357 BB 8 8 Elapsed: 00:00:00.08 satyaki> satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES; MI_PRINX MAPINFO_STYLE_ROW ---------- -------------------------------------------------------------------------------- 9 FFG 8 GGT Elapsed: 00:00:00.06 satyaki> satyaki>update VIEW_MI_JOIN_GC 2 set mapinfo_style_row = 'OOOO' 3 where mi_prinx = 1; 1 row updated. Elapsed: 00:00:00.05 satyaki> satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES; MI_PRINX MAPINFO_STYLE_ROW ---------- -------------------------------------------------------------------------------- 9 OOOO 8 GGT Elapsed: 00:00:00.06 satyaki> satyaki>select mapinfo_style_row from MAP_INDEX; MAPINFO_STYLE_ROW -------------------------------------------------------------------------------- OOOO HHH Elapsed: 00:00:00.06 satyaki>
Hope this will help you.
Kind regards.
LOULOU.
Maybe you are looking for
-
Satellite Pro A40: question charger
I hope that someone may know the answer to that. I have an A40 and over the weekend the charger packed in. Its a powerpack 15v 5amp. I have a spare part for my old Satellite, what do I know works, but not being able to the A40. The rating on this pac
-
How do I re centre of my screen
How do I re centre of my screen
-
get rid of the kiosk - my user account is not displayed.
I bought a hp envy sleekbook with Windows 7, I created for users, one for me and my spouse. The first restart, I did it, I got the Kiosk user I didn't know the password so I chose to change user oddly it only allow the connection of my wife and not m
-
I'M LOOKING FOR MASM AND LINK SOFTWARE FOR WINDOWS 7 64 BIT, PLEASE GIVE ME A LINK?
is it possible to install and run 32-bit masm for my windows 7 64 bit...It is urgent... Please