473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving the Nth record from a one-to-many subrelation table

4 New Member
Using Oracle 10g, I have a "Persons" table and a "Tasks" table, as shown below:

PERSONS Table
=================
PID, FName, LName
-------------------------------
1, John, Smith
2, Bob, Johnson
3, Jane, Walters

TASKS Table
=================
PID, Task#, Task
-------------------------------
1, 1, Change tires
1, 2, Cut grass
1, 3, Watch TV
2, 1, Mop floors
2, 2, Eat sandwich
3, 1, Divide and conquer

The tables are related through the PID field, and each person can have from 0 to 4 tasks assigned to them.

What I need is a query that for any Task # n (where n is 1 to 4), it will return the person's name and Task (or a null value if no Task # n is assigned for that person). For example, if I wanted to see the information for Task # 2, the query should return:

FName, LName, Task
-------------------------------
John, Smith, Cut grass
Bob, Johnsson, Eat sandwich
Jane, Walters, (null)


Any help is greatly appreciated
Apr 24 '08 #1
6 1619
debasisdas
8,127 Recognized Expert Expert
Kindly Post The Query That You Have Tried So Far.
Apr 25 '08 #2
walker3845
4 New Member
I have tried so many bits and pieces, but don't really have anything that has worked. I have tried CASE statements and RANK statements, but can't seem to get it to work. I am a C# developer, so whenever I have needed to "flatten" subrelation data, I have always been inclined to just get the various pieces of data I need separately and put them together in code. Now I am just looking for some more elegant (and faster) ways of retrieving the data.

I'll keep working on it and I'll post anything I have if I get close. Thanks for your help.
Apr 25 '08 #3
walker3845
4 New Member
OK, this is as close as I have gotten so far...

SELECT P.FName, P.LName, CASE WHEN (SELECT COUNT(*) FROM TASKS T WHERE T.PID = P.PID) >= 2 THEN 'Task Name' ELSE null END AS TaskName FROM PERSONS P ORDER BY P.PID

This is intended to return the data for all Persons for Task # 2. the CASE switches correctly, I just can't figure out how to select the actual Task instead of just inserting 'Task Name' (it does return the person's name and a (null) Task if the person has not been assigned a second task).

I tried a RANK function in place of the 'Task Name', but it always returns the second task for the first person (I think I understand why).

Again, I am mainly a developer with very little SQL experience, so I may be way off base with the direction I am going. Oracle may have some analytical functions that do just this thing.

Thanks again.
Apr 25 '08 #4
surisetti
2 New Member
Expand|Select|Wrap|Line Numbers
  1. select fname,lname,nvl(task,'null') from persons p, (select pid,task from tasks where task#=NUMBER OF TASK) tt where tt.pid(+)=p.pid
Apr 26 '08 #5
walker3845
4 New Member
Working perfectly. Thank you.
Apr 28 '08 #6
Kumarswamy
4 New Member
Hi,
Try this .......

select a.pid,a.fname,a.lname,nvl(b.task,'null') from persons a,(select pid,task from tasks where task#=2) b
where a.pid=b.pid(+);
Regards
Kumarswamy



Using Oracle 10g, I have a "Persons" table and a "Tasks" table, as shown below:

PERSONS Table
=================
PID, FName, LName
-------------------------------
1, John, Smith
2, Bob, Johnson
3, Jane, Walters

TASKS Table
=================
PID, Task#, Task
-------------------------------
1, 1, Change tires
1, 2, Cut grass
1, 3, Watch TV
2, 1, Mop floors
2, 2, Eat sandwich
3, 1, Divide and conquer

The tables are related through the PID field, and each person can have from 0 to 4 tasks assigned to them.

What I need is a query that for any Task # n (where n is 1 to 4), it will return the person's name and Task (or a null value if no Task # n is assigned for that person). For example, if I wanted to see the information for Task # 2, the query should return:

FName, LName, Task
-------------------------------
John, Smith, Cut grass
Bob, Johnsson, Eat sandwich
Jane, Walters, (null)


Any help is greatly appreciated
May 2 '08 #7

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

Similar topics

0
2175
by: George Dainis | last post by:
How do I code a SQL SELECT statement so that always only this record is retrieved which matches a certain criteria AND has the lowest ID (= value in key field aaa)? It must me something like ...
1
1192
by: Dave Rams | last post by:
Can some one suggest an article on the saving and retrieving VB.net projects. I have lost complete programs at least twice. I have to reconstruct the programs. It has happened when I enter...
4
1692
by: codewarr2000 | last post by:
Having problem with retrieving a class instance item from a Vector. This is the result of the code below. Also a weird note: If I dont declare as: TYPE_VECTOR_BANKED_MEMORY_DATA...
8
4486
by: Steve | last post by:
Can anyone tell me the preferred method for writing and retrieving persistent information using .Net. Specifically, I am referring to information that you used to see in registry keys or .ini...
2
6748
by: Sven Wynsberghe | last post by:
Like the topic states, is there any way I can get the ip-adress of my computer with a vb.net application? thanks for the help :)
10
4104
by: Bhavna | last post by:
I am using a Replace function to replace single quotes with double when submitting a text field in the database i.e. Replace (q, "'", "' ' ") which works fine. When I retrieve the field from the...
11
2004
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a...
2
2219
by: shivapadma | last post by:
i have inserted the image into database using the following code String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName =...
2
1187
by: SV | last post by:
I have a login form with “Remember me on this computer” checkbox. I am creating cookie if checkbox is checked. My code is: If chk_remember.Checked = True Then Dim ckUserName As HttpCookie =...
0
7231
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
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.