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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gvmk27
Starting Member

44 Posts

Posted - 06/13/2012 :  10:24:40  Show Profile  Reply with Quote
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

India
52326 Posts

Posted - 06/13/2012 :  10:34:46  Show Profile  Reply with Quote

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/


Edited by - visakh16 on 06/13/2012 10:35:47
Go to Top of Page

gvmk27
Starting Member

44 Posts

Posted - 06/13/2012 :  10:49:04  Show Profile  Reply with Quote
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

India
52326 Posts

Posted - 06/13/2012 :  10:50:21  Show Profile  Reply with Quote
wc

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000