Posts Topics Forums Images
Search videos from message boards Videos Search messages from microblogs Microblogs Search messages from imdb.com Imdb Search messages from yuku.com Yuku Search messages from lefora.com (free forums) Lefora
My account: Login | Sign Up
Loading... 

Thread: Selecting ultimo dates of a month

Started 3 weeks, 5 days ago by MartinFalch
Hi there, I have a lot of daily series, with observations on each day. Now I wish to create new tables, containing the same data, but only the last observation of each month. Is there some way to select this ultimo date within SQL, so as to select the 31st when this is the last date, the 30st when this is the last date etc? My code is quite simple, looks like this: ...
Site: dBforums - Database Support Community  dBforums - Database Support Community - site profile
Forum: Microsoft SQL Server  Microsoft SQL Server - forum profile
Total authors: 6 authors
Total thread posts: 18 posts
Thread activity: no new posts during last week
Domain info for: dbforums.com

Other posts in this thread:

MartinFalch Registered User replied 3 weeks, 5 days ago
Hi there, I have a lot of daily series, with observations on each day. Now I wish to create new tables, containing the same data, but only the last observation of each month. Is there some way to select this ultimo date within SQL, so as to select the 31st when this is the last date, the 30st when this is the last date etc? My code is quite simple, looks like this: ...

r937 replied 3 weeks, 5 days ago
WHERE DAY(DATEADD(DAY,1,ObsDateCAD)) = 1

r937 SQL Consultant replied 3 weeks, 5 days ago
WHERE DAY(DATEADD(DAY,1,ObsDateCAD)) = 1

MartinFalch replied 3 weeks, 5 days ago
Arh thanks alot, that was a really smart way to do it! - However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly "flawed" in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method. Problem is that adding an "or" and ...

MartinFalch Registered User replied 3 weeks, 5 days ago
Arh thanks alot, that was a really smart way to do it! - However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly "flawed" in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method. Problem is that adding an "or" and ...

r937 replied 3 weeks, 5 days ago
Quote: Originally Posted by MartinFalch However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly "flawed" in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method....

r937 SQL Consultant replied 3 weeks, 5 days ago
Quote: Originally Posted by MartinFalch However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly "flawed" in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method....

MartinFalch replied 3 weeks, 5 days ago
Hmm seems you are right, it's probably not due to that since it works in some cases, even through the above flaw is there. However, it seems to leave out specific months, 3-4 pr . year. Can it be because the last date of these months in the dataset don't correspond to the actual last date of these years? Meaning that if the code expects the 31st to be the last date and it's the 30st in the ...

MartinFalch Registered User replied 3 weeks, 5 days ago
Hmm seems you are right, it's probably not due to that since it works in some cases, even through the above flaw is there. However, it seems to leave out specific months, 3-4 pr . year. Can it be because the last date of these months in the dataset don't correspond to the actual last date of these years? Meaning that if the code expects the 31st to be the last date and it's the 30st in the ...

Pat Phelan replied 3 weeks, 5 days ago
An NZDF solution might be: Code: SELECT * FROM #ER_CAD AS a WHERE a.ObsDateCAD = (SELECT Max(z.ObsDateCAD) FROM #ER_CAD AS z WHERE DateAdd(m, DateDiff(m, 0, a.ObsDateCAD), 0) <= z.OBSDateCAD AND z.ObsDateCAD < DateAdd(m, 1 + DateDiff(m, 0, a.ObsDateCAD), 0)) -PatP

 

Top contributing authors

Name
Posts
MartinFalch
5
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 09:26:00)
Well those shouldn't be issues, ObsDateCAD is the leftmost column and there shouldn't be any cases of identical dates for this index Again, thanks a lot!
MartinFalch Registered User
5
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 09:26:00)
Well those shouldn't be issues, ObsDateCAD is the leftmost column and there shouldn't be any cases of identical dates for this index Again, thanks a lot!
Pat Phelan
2
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 09:25:00)
Good enough! Note that the code I posted will perform better if there is an index that starts with ObsDateCAD (has that as the leftmost column). It also doesn't deal with &quot;ties&quot;, so if you get two &quot;last&quot; entries with exactly the same ObsDateCAD value in your table, both will be returned in the result set. -PatP
r937
2
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 08:56:00)
Quote: Originally Posted by MartinFalch However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly &quot;flawed&quot; in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method. this makes no difference to my solution however, if you are saying that, for...
Pat Phelan Resident Curmudgeon
2
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 09:25:00)
Good enough! Note that the code I posted will perform better if there is an index that starts with ObsDateCAD (has that as the leftmost column). It also doesn't deal with &quot;ties&quot;, so if you get two &quot;last&quot; entries with exactly the same ObsDateCAD value in your table, both will be returned in the result set. -PatP
r937 SQL Consultant
2
user's latest post:
Selecting ultimo dates of a month
Published (2009-11-13 08:56:00)
Quote: Originally Posted by MartinFalch However, I'm quite dumb for not including this info, but unfortunately the dataset is slightly &quot;flawed&quot; in the sense that it doesn't always have the 1st as the first date of a new month, sometimes the first date is the second or the third - meaning that some months are left out via that method. this makes no difference to my solution however, if you are saying that, for...

Related threads on "dBforums - Database Support Community":

Related threads on other sites:

Thread profile page for "Selecting ultimo dates of a month" on http://www.dbforums.com. This report page is a snippet summary view from a single thread "Selecting ultimo dates of a month", located on the Message Board at http://www.dbforums.com. This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity