Solved

SQL 2005 Group By Month Select Last Value for Month

Posted on 2008-10-30
7
2,957 Views
Last Modified: 2012-06-21
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

Open in new window

0
Comment
Question by:Trinig
  • 4
  • 2
7 Comments
 
LVL 39

Expert Comment

by:appari
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

by:Trinig
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

by:
appari earned 500 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 15

Expert Comment

by:rob_farley
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.

But if you want more information again, I'd recommend using row_number().

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

by:Trinig
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

by:Trinig
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

by:Trinig
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now