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:
...
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:
...
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 ...
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....
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....
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 ...
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
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!
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!
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 "ties", so if you get two "last" entries with exactly the same ObsDateCAD value in your table, both will be returned in the result set. -PatP
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. this makes no difference to my solution however, if you are saying that, for...
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 "ties", so if you get two "last" entries with exactly the same ObsDateCAD value in your table, both will be returned in the result set. -PatP
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. this makes no difference to my solution however, if you are saying that, for...
Related threads on "dBforums - Database Support Community":
Wondering about the person at an expensive hotel who... Wondering about the person at an expensive hotel who actually paid for the reservation system that fails at selecting reservation dates... 4:18 AM Oct 25th from MoodBlast
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