473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to add two result sets.

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.
Jul 9 '10 #1
5 3506
If you have any logic also, reply with that.
Jul 9 '10 #2
amitpatel66
2,367 Expert 2GB
try this query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> SELECT NVL(x.id,y.id),x.hid,y.lid FROM 
  3.   2            (select distinct x.id,hid,row_number() over(partition by x.id order by x.id,hid) rn  
  4. FROM 
  5.   3             www x, wwwh h 
  6.   4             where x.id = h.id) x, 
  7.   5             (SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n 
  8.   6             from www x, wwwl l 
  9.   7             where x.id = l.id) y 
  10.   8             WHERE x.id(+) = y.id 
  11.   9             AND x.rn(+) = y.r_n; 
  12.  
  13. NVL(X.ID,Y.ID)        HID        LID
  14. -------------- ---------- ----------
  15.       14695521   23857780   38562531
  16.       14695521   23857781   38562532
  17.       14695521              38562533
  18.  
  19. SQL> 
  20.  
  21.  
I have used below table names instead of your actuals:

web -> www
web_h -> wwwh
web_s -> wwwl
Jul 12 '10 #3
Amazing amit..... very much thankyou... If possible prefer me some ways to excel in ORACLE....

Thanks for your great help.
Jul 12 '10 #4
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
Jul 12 '10 #5
amitpatel66
2,367 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. 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   
  3. FROM  
  4.                www x, wwwh h  
  5.                where x.id = h.id) x RIGHT OUTER JOIN
  6.                (SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n  
  7.                from www x, wwwl l  
  8.                where x.id = l.id) y  
  9.                ON x.id = y.id  
  10.                AND x.rn = y.r_n
  11. UNION
  12. SELECT NVL(x.id,y.id),x.hid,y.lid FROM  
  13.               (select distinct x.id,hid,row_number() over(partition by x.id order by x.id,hid) rn   
  14. FROM  
  15.                www x, wwwh h  
  16.                where x.id = h.id) x LEFT OUTER JOIN
  17.                (SELECT distinct x.id,lid,row_number() over(partition by x.id order by x.id,lid) r_n  
  18.                from www x, wwwl l  
  19.                where x.id = l.id) y  
  20.                ON x.id = y.id  
  21.                AND x.rn = y.r_n
  22.  
Jul 15 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
5
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...
1
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...
3
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...
1
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...
2
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...
0
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...
2
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...
3
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 :...
6
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>)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.