Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

help regarding selecting the maximum value Expand / Collapse
Author
Message
Posted Thursday, January 10, 2013 6:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105
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
Post #1405382
Posted Thursday, January 10, 2013 6:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:53 AM
Points: 5,218, Visits: 5,072
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
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

Post #1405389
Posted Thursday, January 10, 2013 6:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105
Perfect !!! is it possible to come up with some dynamic query here we are selecting the rows based on the row number(rn)
Post #1405398
Posted Thursday, January 10, 2013 6:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:53 AM
Points: 5,218, Visits: 5,072
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
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

Post #1405400
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse