Solved

# SQL 2005 Group By Month Select Last Value for Month

Posted on 2008-10-30
Medium Priority
2,992 Views
I'm using the "Group By" to group by month. The values I am selecting are 'high', 'low' and 'close'. When I group, I want the highest 'high' (MAX), the lowest low (MIN), and the last close (date wise) in the month. Obviously I have the 'high' and 'low' figured out, MAX() and MIN() respectively, but I'm completely baffled by the 'close' value as there is no LAST() aggregate function. Any help would be appreciated; I've spent too many hours on it already, with no solution coming to me.

``````SELECT convert(datetime, cast(month(A1.Date) as varchar) + '/01/' + cast(year(A1.Date) as varchar), 101) as date,
MAX(high) AS high, MIN(low) AS low, ???? AS close
FROM ( a nice big select )
WHERE date >= #startdate# and date<=#enddate#
GROUP BY YEAR(date), MONTH(date) ORDER BY date
``````
0
Question by:Trinig
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2

LVL 39

Expert Comment

ID: 22847042

try this, the logic is get the last date of the month and if it is same as date value of current record select it otherwise assign null and then get Max out of the all values.

SELECT convert(datetime, cast(month(A1.Date) as varchar) + '/01/' + cast(year(A1.Date) as varchar), 101) as date,
MAX(high) AS high, MIN(low) AS low,
Max(case when
dateadd(m,1,date)- day(date) = date then close else null end)
AS close
FROM ( a nice big select )
WHERE date >= #startdate# and date<=#enddate#
GROUP BY YEAR(date), MONTH(date) ORDER BY date
0

Author Comment

ID: 22847127
I'm not at work atm, so I can't try the solution, but I'm not sure this will work.  With our data, we don't have values for weekends and holidays, so there might not be data for the last day of a month.  If such is the case, I use the 2nd last day, or the 3rd last day, etc until there is data present.
0

LVL 39

Accepted Solution

appari earned 2000 total points
ID: 22847212
in that case you may try something like this using CTE

;
with nice_Select as (a nice big select )
SELECT convert(datetime, cast(month(A1.Date) as varchar) + '/01/' + cast(year(A1.Date) as varchar), 101) as date,
MAX(high) AS high, MIN(low) AS low,
Max(case when EOM is not null then close else null end)
AS close
FROM nice_Select Left Outer join (Select YEAR(date), MONTH(date), Max(date) EOM from nice_Select Group by YEAR(date), MONTH(date) ) as EOMDate
on date = EOM
WHERE date >= #startdate# and date<=#enddate#
GROUP BY YEAR(date), MONTH(date) ORDER BY date

0

LVL 15

Expert Comment

ID: 22847787
I'm not sure why you can't use MAX(date), as if some dates are missing, the MAX could easily be the 29th of the month, or whatever.

You can easily provide a row number for each row, telling it to start again from one for each month. Something like:

select *, row_number() over (partition by year(date), month(date) order by date desc) as rn_backwards
from sometable

No need group by for this - that's what your partition is doing. Now you can select the rows where rn_backwards = 1, to get the final record for the month. Or you can "order by date asc" to get the first row of the month. This is an easy way to get a LAST or FIRST function out of SQL Server 2005.

select * from
(
select *, row_number() over (partition by year(date), month(date) order by date desc) as rn_backwards
from sometable
) t
where rn_backwards = 1; --Gives you the last row for each partition.

Hope this helps,

Rob

-- http://www.lobsterpot.com.au, http://msmvps.com/blogs/robfarley
0

Author Comment

ID: 22849242
Rob,

I understand your method of getting the last date of the month, but I need to use that to get to related close value, which your method would do.  However, I also need the Max(high) and Min(low) which is why I was using the GROUP BY.  Is there a way to incorporate max(high) and min(low) into your row_number partition method?

I appreciate the comments, leaving for work soon so I'll be able to play with some suggestions.

-Ken
0

Author Closing Comment

ID: 31511866
Thanks, the CTE worked great.  Never even thought to look at them.  All I have to do now is construct a weekly version.  Thanks again.
0

Author Comment

ID: 22849975
appari's solution did the trick.  I had never even thought of trying CTE, as they still a bit new to me.  I appreciate the comments and help.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
###### Suggested Courses
Course of the Month9 days, 9 hours left to enroll