Need help with outer joins
I have the following table structure,
Table - 1_
ID | Information
1 | abcadskasasa
2 | asdasdasdasd
3 | saeqdfdvsfcsc
Table - 2_
ID | PID
1 | 12
1 | 13
2 | 14
1 | 15
1 | 16
2 | 12
Table - 3_
ID | PARID
1 | 12
2 | 14
1 | 15
Now I want to select for each ID in table 1, the count of number of PID from table 2 and count of number of PARID from table 3.
Desired output:_
ID | COUNT_PID | COUNT_PARID
1 | 4 | 2
2 | 2 | 1
3 | 0 | 0
Could anyone please help me out with this. I am trying to make use of outer joins, but as I work mostly on the front end so, not able to come up with a proper solution for the above.
Thanks in advance,
Tejas
Hi, Tejas,
You might have been doing the outer join correctly.
There's another problem here: joining table_1 to two other tables with which it has a one-to-many relationship.
If you were joining table_1 to just one other table, you could say:
SELECT t1.id
, COUNT (t2.pid) AS count_pid
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.id = t2.id
GROUP BY t1.id
ORDER BY t1.id;You could have done the exact same thing with table_3 instead of table_2.
But you can't do the same thing with both table_2 and table_3 at the same time: that would be like cross-joining table_2 and table_3. Instead of showing id=1 having count_pid=4 and count_parid=2, you would get cout_pid=8 and count_parid=8 (since 8 = 4 * 2).
You can do a separate GROUP BY on (at least) one of the tables.
This gets the right results. In the main query, there is only one one-to-many relationship.
WITH t3_summary AS
SELECT id
, COUNT (parid) AS count_parid
FROM table_3
GROUP BY id
SELECT t1.id
, COUNT (t2.pid) AS count_pid
, MAX (t3.count_parid) AS count_parid
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.id = t2.id
LEFT OUTER JOIN t3_summary t3 ON t1.id = t3.id
GROUP BY t1.id
ORDER BY t1.id;
Similar Messages
-
Need help with outer join filter.
Need a little help filtering a resultset and I cant seem to find a proper way to do this.
/*table*/
create table invoice( farinvc_invh_code varchar2(100),
farinvc_item varchar2(100),
farinvc_po varchar2(100)
create table po(
supplier_number varchar2(60),
supplier_invoice_no varchar2(60),
po_number varchar2(60),
run_date varchar2(60),
PO_LINE_NUMBER varchar2(60) );
/*data*/
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '1', 'P0142245');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '3', 'P0142245');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '1', 'P0142246');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '2', 'P0142246');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529132260', 'P0142245', '21-NOV-12', '1');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '3');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '2');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145678', 'P0142246', '22-NOV-12', '1');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145679', 'P0142246', '22-NOV-12', '2');query im executing.
SELECT farinvc_invh_code,
supplier_number,
supplier_invoice_no,
farinvc_item,
farinvc_po ,
po_number,
run_date,
PO_LINE_NUMBER
FROM INVOICE, PO
WHERE PO_NUMBER = FARINVC_PO(+)
AND FARINVC_ITEM(+) = PO_LINE_NUMBER
result
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554165" "914100122" "145678" "1" "P0142246" "P0142246" "22-NOV-12" "1"
"I0554165" "914100122" "145679" "2" "P0142246" "P0142246" "22-NOV-12" "2"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2" this is a much larger table and I took an excerpt in order to keep things clear and understanding. I would like to filter this result set to only show the lines that have have the po numbers are the same and line are the same but there is an extra item. in other words like such.
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2"fair enough frank lets add some extra data to the tables.
for example.
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554167', '1', 'P0142447')
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554167', '2', 'P0142447')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV1', 'P0142247', '25-NOV-12', '1')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV2', 'P0142247', '25-NOV-12', '2')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV3', 'P0142247', '25-NOV-12', '3')if we run the query above we get
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2"
"" "914100123" "INV2" "" "" "P0142247" "25-NOV-12" "2"
"" "914100123" "INV1" "" "" "P0142247" "25-NOV-12" "1"
"" "914100123" "INV3" "" "" "P0142247" "25-NOV-12" "3" where really what im trying to target is pos and lines that have a match in both tables and there is additional items from the po table that do not have have a match from the invoice table. Furthermore i would only like to see the items that are repeating invoice numbers, in other words my result here should still only be, because "SUPPLIER_INVOICE_NO" is repeating and it does find a match for the po in the invoice table but yet there is an item with the same invoice and po in the po table that does not have a match in the invoice table.
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2" hope that makes sense, and thanks for working with me on this.
Edited by: mlov83 on Dec 12, 2012 5:53 AM -
Help with outer joins in Oracle!!
so far this is what i've come up with and the code below does not work. Can anyone please help me on how the sytanx of left joins and how to use multiple left joins in a single query in oracle?
SELECT a.*, b.Position_CD, c.Skill_CD, d.Team_Name, d.Team_Country, d.Club, e.Structure_Name
FROM Roster a, Roster_position b, roster_skill c, Team d, Team_Structure e
where (a.Roster_ID = ((b.Roster_ID= c.Roster_Id(+)) b.roster_id(+)).......
I dont' know how to add more left joins!
Here is the query I'm trying to duplicate (which is a query from Ms Access databaase which works fine).
SELECT a.*, b.Position_CD, c.Skill_CD, d.Team_Name, d.Team_Country, d.Club, e.Structure_Name
from ((Roster a LEFT JOIN (Roster_Position b LEFT JOIN Roster_Skill c ON b.Roster_ID=c.Roster_ID) ON a.Roster_ID=b.Roster_ID) LEFT JOIN Team d ON a.Team_CD=d.Team_CD) LEFT JOIN Team_Structure e ON a.Team_Structure_CD=e.Team_Structure_CD
ORDER BY a.Roster_Id;
Any help or comments are greatly appreciatedFirst,
I am not one of the leading SQL brains here...but I'm taking a stab, nonetheless.
Second,
Here's my best guess - and it seems to me that it should work as there are not two outer joins between any two tables. I hope I decoded your joins correctly - quite a mess that Access syntax!
SELECT a.*, b.position_cd, c.skill_cd, d.team_name, d.team_country, d.club, e.structure_name
FROM ROSTER a, ROSTER_POSITION b, ROSTER_SKILL c, TEAM d, TEAM_STRUCTURE e
WHERE a.roster_id = b.roster_id(+)
AND b.roster_id = c.roster_id(+)
AND a.team_cd = d.team_cd(+)
AND a.team_structure = e.team_structure(+);Third,
As an architect-dude, it seems to me that you have a serious
modeling problem to need so many outer joins in such a basic
grab of data. In any scale other than minute, the performance
of this model will suffer dramatically. Specifically, why can't
ROSTER have an equijoin with TEAM and with TEAM_STRUCTURE - as they
appear to be lookup tables...
Good Luck
(I'm certain you'll get better SQL from the others),
Michael O'Neill
Publisher of the PigiWiki
clever-idea.com -
I have two tables:
PS and Entity whose data are as follows
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-DEC-08','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-DEC-08','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-DEC-08','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-DEC-08','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-DEC-08','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('27-JAN-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('27-JAN-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('27-JAN-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('27-JAN-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('27-JAN-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-FEB-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-FEB-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-FEB-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-FEB-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-FEB-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('30-MAR-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('30-MAR-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('30-MAR-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('30-MAR-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('30-MAR-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-APR-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-APR-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-APR-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-APR-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-APR-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('29-MAY-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-JUN-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-JUN-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-JUN-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-JUN-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-JUN-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('28-JUL-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-AUG-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('26-OCT-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('24-NOV-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),9);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),13);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),10);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),6);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),3);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),1);
Insert into PS (DATE_TIME,ENTITY_ID) values (to_timestamp('25-DEC-09','DD-MON-RR HH.MI.SSXFF AM'),14);
Entity:
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (1,'AAA');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (3,'CCC');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (6,'DDD');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (9,'EEE');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (10,'FFF');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (13,'GGG');
Insert into ENTITY (ENTITY_ID,ENTITY_ADDRESS) values (14,'HHH');
I want to display data for each entity available in entity table
What is wrong with this query??
Select ps.date_time,ps.entity_id,et.entity_id from entity et left outer join PS ps on (et.entity_id = ps.entity_id)
This query gives me result like
26-DEC-08 1 1
26-DEC-08 3 3
26-DEC-08 6 6
26-DEC-08 10 10
26-DEC-08 13 13
27-JAN-09 13 13
27-JAN-09 10 10
27-JAN-09 6 6
27-JAN-09 3 3
27-JAN-09 1 1...
But for 26-Dec-08, i want to display
26-DEC-08 1 1
26-DEC-08 3 3
26-DEC-08 6 6
26-DEC-08 10 10
26-DEC-08 13 13
26-DEC-08 NULL 9
26-DEC-09 NULL 14
27-JAN-09 13 13
27-JAN-09 10 10
27-JAN-09 6 6
27-JAN-09 3 3
27-JAN-09 1 1
But the unmatching ids are not displaying when i am using the outer join..please help me..Almost there.
In this case you need a partitioned outer join, partitioning by date_time:
SQL> Select ps.date_time
2 , ps.entity_id
3 , et.entity_id
4 from ps partition by (ps.date_time)
5 right outer join entity et on (et.entity_id = ps.entity_id)
6 /
DATE_TIME ENTITY_ID ENTITY_ID
26-12-08 00:00:00,000000 1 1
26-12-08 00:00:00,000000 3 3
26-12-08 00:00:00,000000 6 6
26-12-08 00:00:00,000000 9
26-12-08 00:00:00,000000 10 10
26-12-08 00:00:00,000000 13 13
26-12-08 00:00:00,000000 14
27-01-09 00:00:00,000000 1 1
27-01-09 00:00:00,000000 3 3
27-01-09 00:00:00,000000 6 6
27-01-09 00:00:00,000000 9
27-01-09 00:00:00,000000 10 10
27-01-09 00:00:00,000000 13 13
27-01-09 00:00:00,000000 14
26-02-09 00:00:00,000000 1 1
26-02-09 00:00:00,000000 3 3
26-02-09 00:00:00,000000 6 6
26-02-09 00:00:00,000000 9
26-02-09 00:00:00,000000 10 10
26-02-09 00:00:00,000000 13 13
26-02-09 00:00:00,000000 14
30-03-09 00:00:00,000000 1 1
30-03-09 00:00:00,000000 3 3
30-03-09 00:00:00,000000 6 6
30-03-09 00:00:00,000000 9
30-03-09 00:00:00,000000 10 10
30-03-09 00:00:00,000000 13 13
30-03-09 00:00:00,000000 14
28-04-09 00:00:00,000000 1 1
28-04-09 00:00:00,000000 3 3
28-04-09 00:00:00,000000 6 6
28-04-09 00:00:00,000000 9
28-04-09 00:00:00,000000 10 10
28-04-09 00:00:00,000000 13 13
28-04-09 00:00:00,000000 14
29-05-09 00:00:00,000000 1 1
29-05-09 00:00:00,000000 3 3
29-05-09 00:00:00,000000 6 6
29-05-09 00:00:00,000000 9 9
29-05-09 00:00:00,000000 10 10
29-05-09 00:00:00,000000 13 13
29-05-09 00:00:00,000000 14 14
26-06-09 00:00:00,000000 1 1
26-06-09 00:00:00,000000 3 3
26-06-09 00:00:00,000000 6 6
26-06-09 00:00:00,000000 9
26-06-09 00:00:00,000000 10 10
26-06-09 00:00:00,000000 13 13
26-06-09 00:00:00,000000 14
28-07-09 00:00:00,000000 1 1
28-07-09 00:00:00,000000 3 3
28-07-09 00:00:00,000000 6 6
28-07-09 00:00:00,000000 9 9
28-07-09 00:00:00,000000 10 10
28-07-09 00:00:00,000000 13 13
28-07-09 00:00:00,000000 14 14
26-08-09 00:00:00,000000 1 1
26-08-09 00:00:00,000000 3 3
26-08-09 00:00:00,000000 6 6
26-08-09 00:00:00,000000 9 9
26-08-09 00:00:00,000000 10 10
26-08-09 00:00:00,000000 13 13
26-08-09 00:00:00,000000 14 14
25-09-09 00:00:00,000000 1 1
25-09-09 00:00:00,000000 3 3
25-09-09 00:00:00,000000 6 6
25-09-09 00:00:00,000000 9 9
25-09-09 00:00:00,000000 10 10
25-09-09 00:00:00,000000 13 13
25-09-09 00:00:00,000000 14 14
26-10-09 00:00:00,000000 1 1
26-10-09 00:00:00,000000 3 3
26-10-09 00:00:00,000000 6 6
26-10-09 00:00:00,000000 9 9
26-10-09 00:00:00,000000 10 10
26-10-09 00:00:00,000000 13 13
26-10-09 00:00:00,000000 14 14
24-11-09 00:00:00,000000 1 1
24-11-09 00:00:00,000000 3 3
24-11-09 00:00:00,000000 6 6
24-11-09 00:00:00,000000 9 9
24-11-09 00:00:00,000000 10 10
24-11-09 00:00:00,000000 13 13
24-11-09 00:00:00,000000 14 14
25-12-09 00:00:00,000000 1 1
25-12-09 00:00:00,000000 3 3
25-12-09 00:00:00,000000 6 6
25-12-09 00:00:00,000000 9 9
25-12-09 00:00:00,000000 10 10
25-12-09 00:00:00,000000 13 13
25-12-09 00:00:00,000000 14 14
91 rijen zijn geselecteerd.Partitioned outer join became available somewhere during version 10. They are described here: http://download.oracle.com/docs/cd/B28359_01/server.111/b28314/tdpdw_sql.htm#TDPDW00736
Regards,
Rob. -
RVS4000: Need help with out-of-the-box device
The router is performing very badly.
1. Router's "INTERNET" is connected to ADSL cable modem, providing automatically an IP address.
2. The device is getting "freezed"/"stoned" very often. The longest time record for up-time was just over 1 hour. The shortest up-time was somewhere over 2 minutes.
3. When "freezed" the router:
a. Still can perform as a switch
b. No access to WAN
c. No ping to the router
d. Green indication lamps.
e. No access to the web-interface
4. Same behavior with 1.3.0.5, 1.3.1.0 and 1.3.2.0 firmwares
5. Sometimes after "Restore to factory defaults" DIAG-RED led is lit, and does not go off.
6. To get device back helps to power-off it for an hour or two.
Is it possible to get any information from the router, when it is "freezed"Pavel Kopylov,
It look like we got out of sync. I see you just sent another reply about opening the box Friday.
With it only being Friday, I would take it back to the store I bought it from and get another. The same or better.
======================================================================
If you want to use this as a learning experiance and figure out this problem yourself, then the following maybe for you:
In all of your communications. I hear you saying your RVS-4000 freezes and will not communicate. No in or out of data. Is that correct?
You are also saying, these freezes occur at various times. Correct?
Which I hope this means is that it is working until it stops working...
If that is correct. Then you need to figure out if the problem is coming in over your WAN connection or it's the hardware of the Router itself.
If you think it's the hardware of the Router. Then do things like measure the temperatures of the Router. (Carefully) Try to heat it or cool it to see if this makes it fail, quicker... Check the Voltage to this Router and make sure it's correct and you are not getting Black outs or brownouts, etc... Also check for lose data connectors. (Watch the lights on the front panel)
However, If you think this is from a TCP/UDP attack on your Router. Then you can place a HUB between your Router and the Cable/DSL Modem and the Router. Next plug in a PC into the same HUB and run Network Software Monitoring on this PC in Continues Record mode. Until a failure. Then go thru the Data and see what you find.
There are a few free Network Software Monitoring Tools you can find and download. Even Microsoft has a nice one for free. Microsoft Network Monitor 3.x
You can also buy another Router. The same or better and if you have the same kind of problems. Then it just might be coming in over your connection. And the new Router can then be returned as its still new.
You can call Cisco and or your local ISP and work with them on this problem.
These are just a few Ideas... I'm sure others will have even more.
Bruce -
Need help with self join query
Hello,
I have table A with the following data
oid parent_oid
10 4
4 2
2 2
12 6
6 6
parent_oid is the parent of oid. I'd like a query that shows the final parent of the oid. The result should show the following
oid final parent
10 2
4 2
2 2
12 6
6 6
I'm using Oracle 10g. I'm familiar with self joins, but that alone will not do the job. Thanks!Hi,
arizona9952 wrote:
... I'm familiar with self joins, but that alone will not do the job.You're absolutely right!
A 2-way self join would work for rows have no parent, or rows that are directly connected to their final ancestor (such as oid=4), but not for anything farther away.
A 3-way self-join would work for one more level away from the final row, but no more. That would be enough for the small set of sample data that you posted, but it would not work if you added a new row with parent_id=10.
An N-way self-join would work for up to N+1 levels, but no more.
You need something that can go any number of levels, such as CONNECT BY:
SELECT CONNECT_BY_ROOT oid AS oid
, parent_oid AS final_parent
FROM a
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY oid = PRIOR parent_oid
AND oid != parent_oid
;Edited by: Frank Kulash on Feb 22, 2010 7:09 PM
Upon sober reflection, I think that a Top-Down query, like the one below, would be more efficient than a Bottom-Up query, like the one above:
SELECT oid
, CONNECT_BY_ROOT parent_oid AS final_parent
FROM a
START WITH parent_oid = oid
CONNECT BY parent_oid = PRIOR oid
AND oid != PRIOR oid
; -
Need help with query joining several tables into a single return line
what i have:
tableA:
puid, task
id0, task0
id1, task1
id2, task2
tableB:
puid, seq, state
id0, 0, foo
id0, 1, bar
id0, 2, me
id1, 0, foo
id2, 0, foo
id2, 1, bar
tableC:
puid, seq, date
id0, 0, 12/21
id0, 1, 12/22
id0, 2, 12/22
id1, 0, 12/23
id2, 0, 12/22
id2, 1, 12/23
what i'd like to return:
id0, task0, 12/21, 12/22, 12/22
id1, task1, 12/23, N/A, N/A
id2, task2, 12/22, 12/23, N/A
N/A doesn't mean return the string "N/A"... it just means there was no value, so we don't need anything in this column (null?)
i can get output like below through several joins, however i was hoping to condense each "id" into a single line...
id0, task0, 12/21
id0, task0, 12/22
id0, task0, 12/23
id1, task1, 12/23
is this possible fairly easily?
Edited by: user9979830 on Mar 29, 2011 10:53 AM
Edited by: user9979830 on Mar 29, 2011 10:58 AMHi,
Welcome to the forum!
user9979830 wrote:
what i have:...Thanks for posting that so clearly!
Whenever you have a question, it's even better if you post CREATE TABLE and INSERT statements for your sample data, like this:
CREATE TABLE tablea
( puid VARCHAR2 (5)
, task VARCHAR2 (5)
INSERT INTO tablea (puid, task) VALUES ('id0', 'task0');
INSERT INTO tablea (puid, task) VALUES ('id1', 'task1');
INSERT INTO tablea (puid, task) VALUES ('id2', 'task2');
CREATE TABLE tablec
( puid VARCHAR2 (5)
, seq NUMBER (3)
, dt DATE -- DATE is not a good column name
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 0, DATE '2010-12-21');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 1, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 2, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id1', 0, DATE '2010-12-23');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 0, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 1, DATE '2010-12-23');This way, people can re-create the problem and test their ideas.
It doesn't look like tableb plays any role in this problem, so I didn't post it.
Explain how you get the results from that data. For example, why do you want this row in the results:
PUID TASK DT1 DT2 DT3
id0 task0 12/21/2010 12/22/2010 12/22/2010rather than, say
PUID TASK DT1 DT2 DT3
id0 task0 12/22/2010 12/21/2010 12/22/2010? Does 12/21 have to go in the first column because it is the earliest date, or is it because 12/21 is related to the lowest seq value? Or do you even care about the order, just as long as all 3 dates are shown?
Always say what version of Oracle you're uisng. The query below will work in Oracle 9 (and up), but starting in Oracle 11, the SELECT ... PIVOT feature could help you.
i can get output like below through several joins, however i was hoping to condense each "id" into a single line... Condensing the output, so that there's only one line for each puid, sounds like a job for "GROUP BY puid":
WITH got_r_num AS
SELECT puid
, dt
, ROW_NUMBER () OVER ( PARTITION BY puid
ORDER BY seq -- and/or dt
) AS r_num
FROM tablec
-- WHERE ... -- If you need any filtering, put it here
SELECT a.puid
, a.task
, MIN (CASE WHEN r.r_num = 1 THEN r.dt END) AS dt1
, MIN (CASE WHEN r.r_num = 2 THEN r.dt END) AS dt2
, MIN (CASE WHEN r.r_num = 3 THEN r.dt END) AS dt3
, MIN (CASE WHEN r.r_num = 4 THEN r.dt END) AS dt4
FROM tablea a
JOIN got_r_num r ON a.puid = r.puid
GROUP BY a.puid
, a.task
ORDER BY a.puid
;I'm guessing that you want the dates arranged by seq; that is, for each puid, the date related to the lowest seq comes first, regardless of whther that date is the earliest date for that puid or not. If that's not what you need, then change the analytic ORDER BY clause.
This does not assume that the seq values are always consecutive integers (0, 1, 2, ...) for each puid. You can skip, or even duplicate values. However, if the values are always consecutive integers, starting from 0, then you could simplify this. You won't need a sub-query at all; just use seq instead of r_num in the main query.
Here's the output I got from the query above:
PUID TASK DT1 DT2 DT3 DT4
id0 task0 12/21/2010 12/22/2010 12/22/2010
id1 task1 12/23/2010
id2 task2 12/22/2010 12/23/2010As posted, the query will display the first 4 dts for each puid.
If there are fewer than 4 dts for a puid, the query will still work. It will leave some columns NULL at the end.
If there are more than 4 dts for a puid, the query will still work. It will display the first 4, and ignore the others.
There's nothing special about the number 4; you could make it 3, or 5, or 35, but whatever number you choose, you have to hard-code that many columns into the query, and always get that many columns of output.
For various ways to deal with a variable number of pivoted coolumns, see the following thread:
PL/SQL
This question actually doesn't have anything to do with SQL*Plus; it's strictly a SQL question, and SQL questions are best posted on the "SQL and PL/SQL" forum:
PL/SQL
If you're not sure whether a question is more of a SQL question or a SQL*Plus question, then post it on the SQL forum. Many more people pay attention to that forum than to this one. -
Need help with Update Join Query
Hello, I am trying to update PID of #child table with PID of #parent table if "lastname & firstname are matches in both table" but my update query is giving some error. Please help and correct the update query. I am also trying to remove any
blank space from starting and ending.
drop table #parent,#child
create table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan ',' Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Update #child
set PID = p.PID
from #child C Join
#parent p ON c.LTRIM(RTRIM(lastname) = p.LTRIM(RTRIM(lastname)
AND c.LTRIM(RTRIM(firstname) = p.LTRIM(RTRIM(firstname)
/* Requested Output */
PID lastname firstname
100 Josheph Sumali
600 Mursan Terrycreate table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan ',' Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Merge #child as t
Using #parent as p ON (LTRIM(RTRIM(t.lastname)) = LTRIM(RTRIM(p.lastname))
AND LTRIM(RTRIM(t.firstname)) = LTRIM(RTRIM(p.firstname)) )
When Matched Then
Update
set PID = p.PID;
update #child
Set lastname=LTRIM(RTRIM(lastname)), firstname= LTRIM(RTRIM(firstname));
update #parent
Set lastname=LTRIM(RTRIM(lastname)), firstname = LTRIM(RTRIM(firstname));
select * from #child
select * from #parent
drop table #parent,#child -
Hi,
I am looking for the following result:
PROVIDER_ID SPECIALTY BUCKET CODE RATING FEE
1 FP EM 100 9 20
1 FP SP 300 15 0
1 INFUS EM 100 3 20
1 INFUS EM 200 6 15The base tables are provided below in the with clause. What I am trying to do is, where "code" matches show the fee from t1 else show 0.
I tried a few variotions but just can't seem to get it right.
with t1 as
select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 200 as code, 15 as fee, 30 as default_multiplier from dual
, t2 as
select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
SELECT t1.provider_id
, t2.specialty
, t2.bucket
, t2.code
, t2.rating
, t1.fee
FROM t1, t2
ORDER BY 1, 2, 3Any help will be appreciated.
Thanks.Could I possibly add one more twist to this.
The current result:
PROVIDER_ID SPECI BU CODE RATING FEE
1 FP EM 100 9 20
1 FP SP 300 15 0
1 INFUS EM 100 3 20
1 INFUS EM 200 6 75
2 FP EM 100 9 40
2 FP SP 300 15 0
2 INFUS EM 100 3 40
2 INFUS EM 200 6 0
3 FP EM 100 9 0
3 FP SP 300 15 0
3 INFUS EM 100 3 0
3 INFUS EM 200 6 60The current code:
with t1 as
select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 200 as code, 75 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 500 as code, 75 as fee, 10 as default_multiplier from dual union all
select 2 as provider_id, 100 as code, 40 as fee, 20 as default_multiplier from dual union all
select 3 as provider_id, 200 as code, 60 as fee, 30 as default_multiplier from dual
, t2 as
select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
, t3 as
SELECT t1.provider_id
, t2.specialty
, t2.bucket
, t2.code
, t2.rating
, CASE t1.code
WHEN t2.code
THEN t1.fee
ELSE 0
END AS fee
, RANK () OVER (PARTITION BY t1.provider_id, t2.specialty, t2.bucket, t2.code ORDER BY CASE t1.code
WHEN t2.code
THEN t1.fee
ELSE 0
END DESC) AS the_rank
FROM t1
, t2
SELECT DISTINCT provider_id
, specialty
, bucket
, code
, rating
, fee
FROM t3
WHERE the_rank = 1
ORDER BY 1
, 2
, 3
, 4I added the code 500 to t1. Howevere, I don't want this code to show up in the result becuase it is does not exist in t2.
I also added code 200 to t1, to see if that shows properly.
I had to do the rank, becuase I need the proper count of rows.
In reality the row counts are:
t1: 20 Million
t2: 10 Thousand.
So, I would like to avoid doing DISTINCT and ANALYTIC functions on this large set, so any better way of achiving the same result, with a better statement.
Thanks. -
Need help with inner join and distinct rows
Hey Guys,
i have
1) BaseEnv Table
2) Link Table
3) BaseData Table
Link table has three columns Id,BaseEnvId,BaseDataId
the BaseEnvID is unique in the table where as BaseDataId can be repeated i.e multile rows of BaseEnv Table can point to same BaseData table row
Now i want to do BaseEnvTable inner join Link Table inner join BaseData Table and select 5 columsn ; Name,SyncName,Version,PPO,DOM from the BaseData table.. the problem is that after i do the inner join I get duplciate records..
i want to eliminate the duplicate records , can any one help me herePlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums. Now we have to guess and type, guess and type, etc. because of your bad manners.
CREATE TABLE Base_Env
(base_env_id CHAR(10) NOT NULL PRIMARY KEY,
Think about the name Base_Data; do you have lots of tables without data? Silly, unh?
CREATE TABLE Base_Data
(base_data_id CHAR(10) NOT NULL PRIMARY KEY,
Your Links table is wrong in concept and implementation. The term “link” refers to a pointer chain structure used in network databases and makes no sense in RDBMS. There is no generic, magic, universal “id” in RDBMS! People that do this are called “id-iots”
in SQL slang.
We can model a particular relationship in a table by referencing the keys in other tables. But we need to know if the relationship is 1:1, 1:m, or n:m. This is the membership of the relationship. Your narrative implies this:
CREATE TABLE Links
(base_env_id CHAR(10) NOT NULL UNIQUE
REFERENCES Base_Env (base_env_id),
base_data_id CHAR(10) NOT NULL
REFERENCES Base_Data (base_data_id));
>> The base_env_id is unique in the table where as base_data_id can be repeated I.e multiple rows of Base_Env Table can point [sic] to same Base_Data table row. <<
Again, RDBMS has no pointers! We have referenced an referencing tables. This is a fundamental concept.
That narrative you posted has no ON clauses! And the narrative is also wrong. There is no generic “name”, etc. What tables were used in your non-query? Replace the ?? in this skeleton:
SELECT ??.something_name, ??.sync_name, ??.something_version,
??.ppo, ??.dom
FROM Base_Env AS E, Links AS L, Base_Data AS D
WHERE ?????????;
>> I want to eliminate the duplicate records [sic], can any one help me here?<<
Where is the sample data? Where is the results? Please read a book on RDBMS so you can post correct SQL and try again.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Need help with conditional join....
Table 1 - Student
ID# Name
1 # A
2 # B
3 # C
4 # D
Table2 - Marks
ID Marks Display
1 # 10 # Y
1 # 20 # Y
1 # 14 # N
2 # 12 # N
2 # 13 # N
3 # 12 # Y
Result...Need query to do this?..
Want to join above two tables and display marks as X when there is no ID in table marks or there is ID but all marked with display as 'N'...if there is one or more
marked with Y then display with marks..
I am using oracle 11i.
ID NAme Marks
1 # A # 10
1 # A # 20
2 # B # X
3 # C # 12
4 # D # XOr, using ANSI join syntax:
with Table1 as (
select 1 id,'A' name from dual union all
select 2,'B' from dual union all
select 3,'C' from dual union all
select 4,'D' from dual
Table2 as (
select 1 id,10 marks,'Y' display from dual union all
select 1,20,'Y' from dual union all
select 1,14,'N' from dual union all
select 2,12,'N' from dual union all
select 2,13,'N' from dual union all
select 3,12,'Y' from dual
-- end of on-the-fly data sample
select t1.id,
name,
nvl(to_char(marks),'X') marks
from Table1 t1
left join
Table2 t2
on (
t2.id = t1.id
and
display = 'Y'
order by id
ID NAME MARKS
1 A 10
1 A 20
2 B X
3 C 12
4 D X
SQL> SY. -
Hi,
I have data like this
create table dd1
(col1 varchar2(20))
insert into dd1
values('P1')
insert into dd1
values('P2')
insert into dd1
values('P3')
insert into dd1
values('P4')
Col1
P1
P2
P3
P4
I need result like this:
Col1 col2
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
Is there any way to this in Sql?
Thanksdd_ram wrote:
Hi,
I have data like this
create table dd1
(col1 varchar2(20))
insert into dd1
values('P1')
insert into dd1
values('P2')
insert into dd1
values('P3')
insert into dd1
values('P4')
Col1
P1
P2
P3
P4
I need result like this:
Col1 col2
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
Is there any way to this in Sql?
ThanksHi,
I have something for you who is more simple like previous example (probably the simplest answer from this forums):
HR: XE > select * from dd1 a, dd1 b
2 where a.col1<b.col1;
COL1 COL1
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
6 rows selected.
HR: XE > Regards,
Ion
Edited by: user111444777 on Sep 26, 2009 7:12 PM
Edited by: user111444777 on Sep 26, 2009 7:18 PM -
Need help with query join...
Hi all, i have the below query, it has too many where clauses and want to seperate it into joins perhaps, how can i do this please suggest some syntax.
Thanks,
Alex
CODE:
“select surname,award.name as award_name,module.name as module_name,attemptno as attempt_no from student,award,studentaward,module,studentenrollment where studentenrollment.attemptno = 2 and studentenrollment.studentid = student.studentid and studentenrollment.moduleid = module.moduleid and studentaward.studentid = student.studentid and studentaward.awardid = award.awardid”select surname,award.name as award_name,module.name as module_name,attemptno as attempt_no
from student
join studentaward on studentaward.studentid = student.studentid
join award on studentaward.awardid = award.awardid
join studentenrollment on studentenrollment.studentid = student.studentid
join module on studentenrollment.moduleid = module.moduleid
where studentenrollment.attemptno = 2 -
Help with outer joins? NVL?
HI,
I have two reports I am creating, one for October and one for november.
The query is below so you can see what I am trying to do. There is a difference of about two hundred accounts, I want to see which accounts were in October and not in November. How can I view the different accounts and list them along side the november accounts or if this can't be done, how can I at least see the accounts which differ between the months? I am thinking that I need to build one report as an inline-view in the from clause, and then do an outerjoin on that aliased table, but am not sure on the syntax. Also, how can I use the NVL function to put in a value for the NULL columns?
select d.month,
a.account_no,
x.first_name||', '||x.last_name,
d.product,
d.cycle_forward ,
d.overusage,
d.logins,
d.seconds/3600 hours
from brio_broadcast.monthly_dial_revenue d,
pin.account_nameinfo_t x,
pin.account_t a
where month = '2002-10'
and a.POID_ID0 = d.ACCOUNT_OBJ_ID0
and a.poid_id0 = x.obj_id0
order by a.account_no;SELECT o.month,
o.account_no,
o.name,
o.product,
o.cycle_forward,
o.overusage,
o.logins,
o.hours
FROM (SELECT d.month,
a.account_no,
x.first_name || ', ' || x.last_name AS name,
d.product,
d.cycle_forward ,
d.overusage,
d.logins,
d.seconds / 3600 hours
FROM brio_broadcast.monthly_dial_revenue d,
pin.account_nameinfo_t x,
pin.account_t a
WHERE d.month = '2002-11'
AND a.poid_id0 = d.account_obj_id0
AND a.poid_id0 = x.obj_id0) n,
(SELECT d.month,
a.account_no,
x.first_name || ', ' || x.last_name AS name,
d.product,
d.cycle_forward ,
d.overusage,
d.logins,
d.seconds / 3600 hours
FROM brio_broadcast.monthly_dial_revenue d,
pin.account_nameinfo_t x,
pin.account_t a
WHERE d.month = '2002-10'
AND a.poid_id0 = d.account_obj_id0
AND a.poid_id0 = x.obj_id0) o
WHERE o.account_no = n.account_no (+)
AND n.account_no IS NULL
UNION ALL
SELECT n.month,
n.account_no,
n.name,
n.product,
n.cycle_forward,
n.overusage,
n.logins,
n.hours
FROM (SELECT d.month,
a.account_no,
x.first_name || ', ' || x.last_name AS name,
d.product,
d.cycle_forward ,
d.overusage,
d.logins,
d.seconds / 3600 hours
FROM brio_broadcast.monthly_dial_revenue d,
pin.account_nameinfo_t x,
pin.account_t a
WHERE d.month = '2002-10'
AND a.poid_id0 = d.account_obj_id0
AND a.poid_id0 = x.obj_id0) o,
(SELECT d.month,
a.account_no,
x.first_name || ', ' || x.last_name AS name,
d.product,
d.cycle_forward ,
d.overusage,
d.logins,
d.seconds / 3600 hours
FROM brio_broadcast.monthly_dial_revenue d,
pin.account_nameinfo_t x,
pin.account_t a
WHERE d.month = '2002-11'
AND a.poid_id0 = d.account_obj_id0
AND a.poid_id0 = x.obj_id0) n
WHERE n.account_no = o.account_no (+)
AND o.account_no IS NULL
ORDER BY 2 -
I need help with a PDF file that is an image, the "Read Out Loud' option does not work, please help!
You mean an image such as a scanned document?
If that is the case, the file doesn't contain any text for Reader Out Loud to read. In order to fix that, you would need an application such as Adobe Acrobat that does Optical Character Recognition to convert the images to actual text.
You can also try to export the file as a Word document or something else using ExportPDF which I believe offers OCR and is cheaper than Acrobat.
Maybe you are looking for
-
Error finding/creating AM from Java Concurrent Program
Hi All, Here is what I am attempting to do in a Java concurrent program --------------- Code Start - Error description in the code snippet comments --------------- public void runProgram(CpContext pCpContext) DBTransactionImpl mDBTransactionImpl = ne
-
Source not determined for MRP generated PR
Hi, I am not getting my source determined after the MRP is run for the Purchase requisions.That means I am not getting the Source termination tab activated and source of supply identified in the PR created after MRP run. Eventually I am unable to con
-
Why won't my ipad charge from my computer when my iphone does? also where have my facetime and contacts icons gone?
-
Seems an update has happened - why? Any how the bookmark orange box in the top lh corner of the toolbar has gone missing. So how do I now bookmark a website for future use? Second how do I display my list of bookmarks? Replies in language a 70 year o
-
JDeveloper 9i JSP Error Handling
I have created a JSP application using JDeveloper 9i BC4J JSP Wizards. This question relates to the pages created by the "Browse & Edit Form" Wizard. If a data error is encountered when the update button is pressed on the "Edit" page, the error is di