Solved

SQL script to return only one row per persnbr

Posted on 2011-09-14
9
336 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

914 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

14 Experts available now in Live!

Get 1:1 Help Now