hi,
I have three tables named WEB, WEB_H, WEB_S. Fields in WEB are id and some other. Fields in WEB_H are id, h_id and some other. Fields in WEB_S are id, s_id and some other. In the three tables, id represent a same entity. WEB table is base table. For an id in WEB table, the matched datas may or may not be present in both, table WEB_S and WEB_H. But it is sure that atleast either of the two tables web_s or web_h has matched datas for id of WEB. What I tried to do is,
For id 14695521 from WEB, I get the data from WEB_H using left outer join and got the result as
14695521 23857780
14695521 23857781
For the same id from WEB, I get the data from WEB_S using left outer join and got the result as
14695521 38562531
14695521 38562532
14695521 38562533
I want to simply combine the result as shown below
(Manually typed)
14695521 23857780 38562531
14695521 23857781 38562532
14695521 (null) 38562533
I tried the below two queries:
1. select distinct * from
(
select
c.id,c.h_id,d.s_id from
(select distinct * from
(select
a.id id, b.h_id h_id
from
web a left join web_h b on a.id=b.id
where a.id='14695521')) c left join web_s d on c.id=d.id
)
2. select distinct c.id, c.h_id, d.s_id from
(
select distinct a.id id, b.h_id h_id from
web a,
web_h b
where aid=b.id(+)
and a.id='14695521'
) c, web_s d
where c.id= d.id(+)
But both the queries return the result as below
14695521 23857781 38562531
14695521 23857781 38562532
14695521 23857780 38562532
14695521 23857781 38562533
14695521 23857780 38562531
14695521 23857780 38562533
Please help me to get the result as I mentioned earlier.
5 3506
If you have any logic also, reply with that.
try this query: -
-
SQL> SELECT NVL(x.id,y.id),x.hid,y.lid FROM
-
2 (select distinct x.id,hid,row_number() over(partition by x.id order by x.id,hid) rn
-
FROM
-
3 www x, wwwh h
-
4 where x.id = h.id) x,
-
5 (SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n
-
6 from www x, wwwl l
-
7 where x.id = l.id) y
-
8 WHERE x.id(+) = y.id
-
9 AND x.rn(+) = y.r_n;
-
-
NVL(X.ID,Y.ID) HID LID
-
-------------- ---------- ----------
-
14695521 23857780 38562531
-
14695521 23857781 38562532
-
14695521 38562533
-
-
SQL>
-
-
I have used below table names instead of your actuals:
web -> www
web_h -> wwwh
web_s -> wwwl
Amazing amit..... very much thankyou... If possible prefer me some ways to excel in ORACLE....
Thanks for your great help.
Got a problem buddy.....
We dont know whether WEB_H or WEB_S will have more datas for the id of WEB. When the above code is executed, it works well for the id which has more number of datas in WEB_S. That is, im getting the actual result for the scenario i explained above..
If WEB_H has more datas.
For ex. Im changing the above scenario.
For id 14690001 from WEB, I get the data from WEB_H using left outer join and got the result as
14690001 38562531
14690001 38562532
14690001 38562533
For the same id from WEB, I get the data from WEB_S using left outer join and got the result as
14690001 23857780
14690001 23857781
If I execute the above code for this scenario, Im getting the result as
14690001 38562531 23857780
14690001 38562532 23857781..
As you see, the last data of WEB_H is omitted...
Please advice
You will need to do the LEFT and RIGHT OUTER JOIN and UNION SET will take care of the duplicates. FULL OUTER JOIN will not work as expected.
Try this: -
SELECT NVL(x.id,y.id),x.hid,y.lid FROM
-
(select distinct x.id,hid,row_number() over(partition by x.id order by x.id,hid) rn
-
FROM
-
www x, wwwh h
-
where x.id = h.id) x RIGHT OUTER JOIN
-
(SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n
-
from www x, wwwl l
-
where x.id = l.id) y
-
ON x.id = y.id
-
AND x.rn = y.r_n
-
UNION
-
SELECT NVL(x.id,y.id),x.hid,y.lid FROM
-
(select distinct x.id,hid,row_number() over(partition by x.id order by x.id,hid) rn
-
FROM
-
www x, wwwh h
-
where x.id = h.id) x LEFT OUTER JOIN
-
(SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n
-
from www x, wwwl l
-
where x.id = l.id) y
-
ON x.id = y.id
-
AND x.rn = y.r_n
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Darko Jovisic |
last post by:
Hi!
Another silly question:
If a stored procedure returns multiple result sets, how do I choose the one
I want to insert into a table?
For example sp_spaceused returns two result sets if...
|
by: Stanley Sinclair |
last post by:
I have a need to return multiple result sets from a stored procedure.
Want that SP to call others to get the data.
Win2003, db2 8.1.5.
Can't figure out how to handle open cursors, and return...
|
by: Arijit Chatterjee |
last post by:
Dear Faculties,
I have a query on this statement..
===============================
CREATE PROCEDURE Check_Manage ( )
DYNAMIC RESULT SETS 1
==============================
I want to know the...
|
by: Michael C# |
last post by:
Hi all,
I'm sending a command via SqlClient, and it returns two result sets. I can
successfully read the first result set, but how can I access the second
result set? Here's an example of my...
|
by: randall g |
last post by:
I have a stored procedure which returns multiple result sets, enclosing
each in its own tag. This works in ADO but not ADO.NET, where an error
is returned by ExecuteXmlReader:
"Invalid command...
|
by: Alec |
last post by:
First attempt at doing an exercise on paginating result sets
When I run the code, I receive the error "Fatal error: Call to
undefined function: mysql_fetch_objects() in...
|
by: prad |
last post by:
Hi,
Following java code returns 24 result sets.It counts number of rows in the first result set correctly.But doesnt count rows from next result set.
When I debugged the code I found out that...
|
by: Crash |
last post by:
SQL Server 2005 SP1 Standard & Express
..NET 2.0 and ADO.NET
C#
Hi,
I have a stored procedure that returns results from 3 select
statements. When I "ExecuteDataset" from C# code the...
|
by: esmith2112 |
last post by:
Scratching my head on an issue that has been plaguing us ever since we
upgraded from version to version 8. Our db2diag file gets inundated
with messages that take the form:
MESSAGE :...
|
by: happyhondje |
last post by:
Hello everyone,
I've got a little issue, both programming and performance-wise. I have
a set, containing objects that refer to other sets. For example, in a
simple notation: (<a, b, c>, <d, e>)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |