SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PersonID with Max(Version) from it...


PersonID with Max(Version) from it...

Author
Message
info 58414
info 58414
Say Hey Kid
Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)Say Hey Kid (696 reputation)

Group: General Forum Members
Points: 696 Visits: 357
Hi,
how can I get effectively max(version) to identify any PersonID with multiple VersionID?
PersonID version
1 1
1 2
1 3
2 1
2 2
3 1
4 1
4 2
and so on ...
Result
1 3
2 2
3 1
4 2

Thank you
Nicole
Smile
mtassin
mtassin
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7234 Visits: 72521
info 58414 (8/9/2012)
Hi,
how can I get effectively max(version) to identify any PersonID with multiple VersionID?


That's what GROUP BY and MAX are for Smile


DECLARE @persons TABLE(
personid INT,
version INT)

INSERT @persons
VALUES(1,1),(1,2),(1,3),(2,1),(2,2),(3,1),(4,1),(4,2)

SELECT
personid,
maxversion = MAX(version)
FROM
@persons
GROUP BY personid
ORDER BY personid





--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94357 Visits: 38955
Curious, which is the more scalable solution?


DECLARE @persons TABLE(
personid INT,
version INT);

INSERT @persons
VALUES(1,1),(1,2),(1,3),(2,1),(2,2),(3,1),(4,1),(4,2);

SELECT
personid,
maxversion = MAX(version)
FROM
@persons
GROUP BY personid
ORDER BY personid;

WITH BaseData AS (
SELECT
personid,
version,
ROW_NUMBER() OVER (PARTITION BY personid ORDER BY version DESC) rn
FROM
@persons
)
SELECT
personid,
version
FROM
BaseData
where
rn = 1;




I guess I'll have to test this at home tonight.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94357 Visits: 38955
Looking at the execution plan, group by version looks simplier, 4 operators versus 6. The table scan and sort have the same cost associated in both.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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