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


Only show the latest date in a selection of data


Only show the latest date in a selection of data

Author
Message
jon.clay 91459
jon.clay 91459
SSC Veteran
SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)

Group: General Forum Members
Points: 261 Visits: 16
Hi there everyone

I'm having problems using the MaxDate function in SQL and wondered if you could please help me?

I have the following scenario whereby a ContactID field can appear several times and yet have different lines of SQL that show different renewal dates. This is for a membership organisation that has a different renewal date each year, just so you get the picture. So, I might have something like this:

ContactID RenewalDate
1 01/07/2018
1 01/07/2017
1 01/07/2016
2 01/07/2015
2 01/07/2014
3 01/07/2018
3 01/07/2017
3 01/07/2016

I'm trying to create a statement that will run through the Subscriptions table and only pull out the highlighted entries shown above (i.e. the latest date for each ContactID).

I've tried various SQL scripts but none of them seem to work.

Many thanks
Jon
GilaMonster
GilaMonster
SSC Guru
SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)

Group: General Forum Members
Points: 859598 Visits: 48576
Please post table definitions and sample data (as INSERT statements) if you want a query written.

If you just want advice, then you can use ROW_NUMBER, partition by ContactID Order by RenewalDate DESC and filter for that = 1

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jon.clay 91459
jon.clay 91459
SSC Veteran
SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)

Group: General Forum Members
Points: 261 Visits: 16
Thanks Gail, but I'm not sure how to achieve that.
I've been doing this (which I thought might work), but I still get multiple rows per ContactID

SELECT ContactID, RenewalDate
FROM SubscriptionMembers
WHERE RenewalDate IN
(SELECT MAX(RenewalDate)
FROM SubscriptionMembers
GROUP BY ContactID)
ORDER BY ContactID
GilaMonster
GilaMonster
SSC Guru
SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)SSC Guru (859K reputation)

Group: General Forum Members
Points: 859598 Visits: 48576
GilaMonster - Tuesday, January 2, 2018 8:38 AM
Please post table definitions and sample data (as INSERT statements) if you want a query written.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)

Group: General Forum Members
Points: 339739 Visits: 34000
Here's a set of methods on how you can deal with versioned data. Instead of a GROUP BY, I'd suggest using a TOP 1 with an ORDER BY. That's generally going to result in superior performance.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
jon.clay 91459
jon.clay 91459
SSC Veteran
SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)SSC Veteran (261 reputation)

Group: General Forum Members
Points: 261 Visits: 16
Many thanks for your help everyone.

Best wishes
Jon
below86
below86
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7581 Visits: 4890
You should be able to use the MAX to get what you want, unless I'm not understanding your issue.

DECLARE  @sample TABLE
(
ContactID INT,
RenewalDate DATE
)
;

INSERT INTO @sample
SELECT 1, '01/07/2018'
UNION ALL
SELECT 1, '01/07/2017'
UNION ALL
SELECT 1, '01/07/2016'
UNION ALL
SELECT 2, '01/07/2015'
UNION ALL
SELECT 2, '01/07/2014'
UNION ALL
SELECT 3, '01/07/2018'
UNION ALL
SELECT 3, '01/07/2017'
UNION ALL
SELECT 3, '01/07/2016'

SELECT ContactID, MAX(RenewalDate) AS RenewalDate
FROM @sample
GROUP BY ContactID
ORDER BY ContactID
Results:
ContactID RenewalDate
1 2018-01-07
2 2015-01-07
3 2018-01-07


-------------------------------------------------------------
we travel not to escape life but for life not to escape us
saravanatn
saravanatn
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1662 Visits: 1149
jon.clay 91459 - Tuesday, January 2, 2018 8:36 AM
Hi there everyone

I'm having problems using the MaxDate function in SQL and wondered if you could please help me?

I have the following scenario whereby a ContactID field can appear several times and yet have different lines of SQL that show different renewal dates. This is for a membership organisation that has a different renewal date each year, just so you get the picture. So, I might have something like this:

ContactID RenewalDate
1 01/07/2018
1 01/07/2017
1 01/07/2016
2 01/07/2015
2 01/07/2014
3 01/07/2018
3 01/07/2017
3 01/07/2016

I'm trying to create a statement that will run through the Subscriptions table and only pull out the highlighted entries shown above (i.e. the latest date for each ContactID).

I've tried various SQL scripts but none of them seem to work.

Many thanks
Jon

Is this your requirement?


create table renewal
(
contactid int,
renewaldate date
);
insert into renewal values (1,'01/07/2016');
insert into renewal values (1,'01/07/2017');
insert into renewal values (1,'01/07/2018');
insert into renewal values (2,'01/07/2014');
insert into renewal values (2,'01/07/2015');
select
contactid,
MAX(renewaldate)
from renewal
group by contactid


Saravanan
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