SQL Clone
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
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1479 Visits: 1392
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
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63120 Visits: 8598
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





How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Smash125
Smash125
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1479 Visits: 1392
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
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63120 Visits: 8598
What do you want to make dynamic?

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



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
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