Solved

SQL script to return only one row per persnbr

Posted on 2011-09-14
9
335 Views
Last Modified: 2012-05-12
I am trying to put together a script to return only the highest numbered IDNbr from the data below for each person.  How would I script that?  Thanks.

PersNbr      IDNbr      DateLastMaint
1      1      8/1/2011
1      2      8/20/2011
1      3      9/5/2011
2      1      8/15/2011
2      1      8/18/2011
3      1      9/1/2011
4      1      8/5/2011
4      2      9/12/2011
0
Comment
Question by:GBTIS
9 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 400 total points
ID: 36537281
select pers, nbr, idnbr, datelastmaint from
(
select pers, nbr, idnbr, datelastmaint, row_number() over(partition by pers order by idnbr desc) myrownum from table
) where myrownum=1
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 50 total points
ID: 36537283
s elect persnbr,max(idnbr) from yourtable
group by persnbr
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 50 total points
ID: 36537296
SELECT
	PersNbr,
	MAX(IDNbr) MaxIDNbr
FROM
	table
GROUP BY
	PersNbr

Open in new window


will return the highest IDNbr per PersNbr.

What do you want to do with the DateLastMaint?

Do you have an ID column?
0
 

Author Comment

by:GBTIS
ID: 36537437
slightwv,

Your solution seems to work the best seeing as though I have multiple columns from multiple tables.  Can you help me get your query into my existing query.  Basically it goes like this.

Your Query Modified:

select cardnbr, currentstatus, idnbr, datelastmaint from
(
select cardnbr, currentstatus, idnbr, datelastmain, row_number() over(partition by cardnbr order by idnbr desc) myrownum from cardissue
) where myrownum=1

Exisitng Query to Combine With

select a.cardnbr, b.accountnbr, c.persnbr, d.currentstatus, e.accountstatus, e.office, e.phone
from card a, cardpers b, pers c, cardissue d, account e
where a.cardnbr=b.cardnbr and b.persnbr = c.persnbr and b.cardnbr=d.cardnr and b.accountnbr=e.accountnbr and b.cardtype<>'CARD'
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36537464
>>Exisitng Query to Combine With


Where is idnbr in that query?

Anyway you should just be able to take what you have and add the row_number function call using the proper columns in the query.
0
 

Author Comment

by:GBTIS
ID: 36537473
The idnbr would be in the cardissue (d) table.  I don't really need it in the output I just want the record with the highest value.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36537517
Untested (just typed in):

select cardnbr, accountnbr, persnbr, currentstatus, accountstatus, office, phone from (
select a.cardnbr, b.accountnbr, c.persnbr, d.currentstatus, e.accountstatus, e.office, e.phone, row_number() over(partition by  c.persnbr order by d.idnbr) myrownum
from card a, cardpers b, pers c, cardissue d, account e
where a.cardnbr=b.cardnbr and b.persnbr = c.persnbr and b.cardnbr=d.cardnr and b.accountnbr=e.accountnbr and b.cardtype<>'CARD'
)
where myrownum=1
0
 

Author Comment

by:GBTIS
ID: 36537625
I was able to get that to work.  Thank you very much.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36537637
Glad to help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle regular expression 6 30
PL/SQL LOOP CURSOR 3 40
Update foreign key reference after insert 9 35
SQL Select Query help 3 31
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now