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.
| Author |
Topic |
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-05-06 : 12:04:53
|
| I'm trying to get the following results and below the results is my query along with the workcalendar table used in the query. My problem is that I can't get the correct Posting Date Range. It would also be fine if just the last posting date was available.4/19/2010 Posting Year 2010Posting Month 4Posting Day 12Total Posting Days 22Posting Date Range 4/16/2010 - 4/16/20104/20/2010 Posting Year 2010Posting Month 4Posting Day 13Total Posting Days 22Posting Date Range 4/17/2010 - 4/19/2010DECLARE @date DATETIMESELECT @date = '4/19/2010'SELECT year (CONVERT(VARCHAR(10),@date-1,101)) as PostingYear,month (CONVERT(VARCHAR(10),@date-1,101)) as PostingMonth,(SELECT COUNT(*)FROM workcalendarWHERE dt BETWEEN C.first AND C.PostDateAND isweekday = 1) AS BusinessDay,(SELECT COUNT(*)FROM workcalendarWHERE dt BETWEEN C.first AND C.lastAND isweekday = 1) AS TotalBusinessDays,c.PostDateFROM(SELECT CONVERT(VARCHAR(10),DATEADD(dd,-(DAY(@date)-1),@date),101) as First,CONVERT(VARCHAR(10),@date-1,101) as PostDate,CONVERT(VARCHAR(10),DATEADD(dd,-(DAY(DATEADD(mm,1,@date))),DATEADD(mm,1,@date)),101) as Last) CQUERY RESULTS:PostingYear PostingMonth BusinessDay TotalBusinessDays2010 4 12 22PostDate 04/18/2010WORKCALENDAR TABLE:dt isWeekDay isWorkDay2010-04-01 00:00:00 1 12010-04-02 00:00:00 1 12010-04-03 00:00:00 0 02010-04-04 00:00:00 0 02010-04-05 00:00:00 1 12010-04-06 00:00:00 1 12010-04-07 00:00:00 1 12010-04-08 00:00:00 1 12010-04-09 00:00:00 1 12010-04-10 00:00:00 0 02010-04-11 00:00:00 0 02010-04-12 00:00:00 1 12010-04-13 00:00:00 1 12010-04-14 00:00:00 1 12010-04-15 00:00:00 1 12010-04-16 00:00:00 1 12010-04-17 00:00:00 0 02010-04-18 00:00:00 0 02010-04-19 00:00:00 1 12010-04-20 00:00:00 1 12010-04-21 00:00:00 1 12010-04-22 00:00:00 1 12010-04-23 00:00:00 1 12010-04-24 00:00:00 0 02010-04-25 00:00:00 0 02010-04-26 00:00:00 1 12010-04-27 00:00:00 1 12010-04-28 00:00:00 1 12010-04-29 00:00:00 1 12010-04-30 00:00:00 1 1 |
|
|
|
|
|
|
|