Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query

Author  Topic 

gvmk27
Starting Member

44 Posts

Posted - 2012-06-13 : 10:24:40
Hi

I have table with 2 column's
1. calendardate(date)
2. GroupId(int)

Consecutive dates will be stored with groupid, and
date will be stored in mm/dd/yyyy format.

01/01/2012 1
01/02/2012 1
01/03/2012 1
02/01/2012 2
02/02/2012 2
02/03/2012 2
06/11/2012 3
06/12/2012 3
06/13/2012 3
06/21/2012 4
06/22/2012 4
06/23/2012 4

I need to write a sql which displays start date and end date of each group. If end date of a group is less than today's date, I should not display that record.

so the output of above table should be

06/11/2012 - 06/13/2012
06/21/2012 - 06/23/2012

I was trying like this, but not getting expected result.

SELECT GroupId, min(CalendarDate) as StartDate, max(CalendarDate) as EndDate
FROM CenterDateExclusionSetting
WHERE CenterGUID = @CenterGUID
AND CalendarDate > GETDATE()
GROUP BY GroupId

Thanks for your help in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-13 : 10:34:46
[code]
SELECT GroupId, min(CalendarDate) as StartDate, max(CalendarDate) as EndDate
FROM CenterDateExclusionSetting
WHERE CenterGUID = @CenterGUID
GROUP BY GroupId
HAVING max(CalendarDate) >= CAST(GETDATE() AS date)
[/code]



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gvmk27
Starting Member

44 Posts

Posted - 2012-06-13 : 10:49:04
Thanks Visakh..

quote:
Originally posted by visakh16


SELECT GroupId, min(CalendarDate) as StartDate, max(CalendarDate) as EndDate
FROM CenterDateExclusionSetting
WHERE CenterGUID = @CenterGUID
GROUP BY GroupId
HAVING max(CalendarDate) >= CAST(GETDATE() AS date)




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-13 : 10:50:21
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -