Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


help regarding selecting the maximum value


help regarding selecting the maximum value

Author
Message
Smash125
Smash125
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 1381
Please find the test data as below.finding tough to come up with logic.

Would appreciate greatly if some could help

create table #data
(
contactid int,
totaldue money,
rn int,
maxrn int
)
select 1, 49846.693, 1, 1
union all
select 1, 43214.9511, 2, 2
union all
select 2, 43962.7901, 1, 1
union all
select 2, 42123.1691, 2, 2
union all
select 3, 89409.6319, 1, 1
union all
select 3, 82078.0355, 2, 2
union all
select 4, 27162.5876, 1, 1

I want the o/p like

contactid,totaldue,rn,maxrn

1,43214,2,2

2,42123.1691,2,2

3,82078.0355,2,2

4,27162.5876,1,1
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6102 Visits: 6078
A good case for ROW_NUMBER()


drop table #data

create table #data
(
contactid int,
totaldue money,
rn int,
maxrn int
)
insert into #data
select 1, 49846.693, 1, 1
union all
select 1, 43214.9511, 2, 2
union all
select 2, 43962.7901, 1, 1
union all
select 2, 42123.1691, 2, 2
union all
select 3, 89409.6319, 1, 1
union all
select 3, 82078.0355, 2, 2
union all
select 4, 27162.5876, 1, 1

select * from #data

;with cte as
(
select
ROW_NUMBER() OVER(PARTITION BY ContactID ORDER BY RN DESC) AS RowNum,
*
FROM
#data
)
select * from cte where rownum = 1





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Smash125
Smash125
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 1381
Perfect !!! is it possible to come up with some dynamic query here we are selecting the rows based on the row number(rn)
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6102 Visits: 6078
What do you want to make dynamic?

But yes if you write dynamic SQL then you can make it dynamic.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search