﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Stewart Campbell  / Obtaining First and Last Values in a Group / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 13:35:22 GMT</lastBuildDate><ttl>20</ttl><item><title>Obtaining First and Last Values in a Group</title><link>http://www.sqlservercentral.com/Forums/Topic1086180-1684-1.aspx</link><description>I am trying to get data for a week.  I am getting the min and max for a couple of fields but also want to get the first and last values for a couple of columns.  I am using a query to group by week and can get the min and max but not sure how to obtain the first and last values.  I had tried joining the table on itself, but to no avial.Below is the query... Any insight is greatly appreciated!!SELECT	MIN(q1.[QUOTE_DATE]) AS STARTDATE,		MAX(q1.[QUOTE_DATE]) AS ENDDATE,	--&amp;gt;	FIRST(q2.[OPEN]) AS OPENPRICE,		MAX(q1.[HIGH]) AS HIGH,		MIN(q1.[LOW]) AS LOW,		--&amp;gt;	LAST(q1.[CLOSE]) AS CLOSEPRICE	FROM	[dbo].[quote] as q1	WHERE	q1.[SECURITY_ID] = @securityId	AND		q1.[QUOTE_DATE] BETWEEN @fromDate AND @toDate	GROUP BY DATEPART(WEEK,q1.[QUOTE_DATE])	ORDER BY 1 ASC</description><pubDate>Wed, 30 Mar 2011 06:59:45 GMT</pubDate><dc:creator>donald.hook</dc:creator></item></channel></rss>