Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Counting weekdays within a date range and grouping them by month Expand / Collapse
Author
Message
Posted Friday, June 07, 2013 8:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 10, 2013 6:09 AM
Points: 1, Visits: 5
Hi Folks,

I was hoping I can get some help and your expertise on below reuirement.

I have this data where first two columns represent the date range and next seven (event1, event2 etc) represent the incidence occurance by day monday thru sunday. which is represented as values 0 and 1. (1 being the event occuring and 0 no event). I need to count event occurance and group them by month.

FROM TO Event1 Event2 Event3 Event4 Event5 Event6 Event7
29-Apr-13 01-May-13 1 0 1 0 0 0 0
30-Apr-13 25-Oct-13 1 1 0 1 1 1 1
08-May-13 11-Sep-13 0 0 1 0 0 0 0
18-Sep-13 18-Sep-13 0 0 1 0 0 0 0
25-Sep-13 23-Oct-13 0 0 1 0 0 0 0
26-Oct-13 26-Oct-13 0 0 0 0 0 1 0
27-Oct-13 28-Mar-14 1 1 1 1 1 1 1
29-Mar-14 29-Mar-14 0 0 0 0 0 1 0
30-Mar-14 24-Oct-14 1 1 1 1 1 0 1
05-Apr-14 18-Oct-14 0 0 0 0 0 1 0

The results I am expecting are as per the attachment.

Is anyone able to help.


  Post Attachments 
image.png (15 views, 26.88 KB)
Post #1461117
Posted Friday, June 07, 2013 9:01 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, April 18, 2014 2:24 PM
Points: 11,990, Visits: 11,007
Hi and welcome to the forums. It is very unclear what you are trying to do here.

In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1461125
Posted Sunday, June 09, 2013 9:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:17 AM
Points: 1,642, Visits: 16,578
may be this will get you started....
you will also need a "Tally" table...search this site for guides

suggest you read up on dynamic pivot/cross tabs as well



CREATE TABLE [dbo].[TransData](
[Fromdate] [datetime] NOT NULL,
[ToDate] [datetime] NOT NULL,
[Event1] [smallint] NOT NULL,
[Event2] [smallint] NOT NULL,
[Event3] [smallint] NOT NULL,
[Event4] [smallint] NOT NULL,
[Event5] [smallint] NOT NULL,
[Event6] [smallint] NOT NULL,
[Event7] [smallint] NOT NULL
) ON [PRIMARY]

GO

INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Apr 29 2013 12:00:00:000AM','May 1 2013 12:00:00:000AM',1,0,1,0,0,0,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Apr 30 2013 12:00:00:000AM','Oct 25 2013 12:00:00:000AM',1,1,0,1,1,1,1)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('May 8 2013 12:00:00:000AM','Sep 11 2013 12:00:00:000AM',0,0,1,0,0,0,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Sep 18 2013 12:00:00:000AM','Sep 18 2013 12:00:00:000AM',0,0,1,0,0,0,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Sep 25 2013 12:00:00:000AM','Oct 23 2013 12:00:00:000AM',0,0,1,0,0,0,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Oct 26 2013 12:00:00:000AM','Oct 26 2013 12:00:00:000AM',0,0,0,0,0,1,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Oct 27 2013 12:00:00:000AM','Mar 28 2014 12:00:00:000AM',1,1,1,1,1,1,1)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Mar 29 2014 12:00:00:000AM','Mar 29 2014 12:00:00:000AM',0,0,0,0,0,1,0)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Mar 30 2014 12:00:00:000AM','Oct 24 2014 12:00:00:000AM',1,1,1,1,1,0,1)
INSERT INTO [transdata] ([Fromdate],[ToDate],[Event1],[Event2],[Event3],[Event4],[Event5],[Event6],[Event7])VALUES('Apr 5 2014 12:00:00:000AM','Oct 18 2014 12:00:00:000AM',0,0,0,0,0,1,0)

SET DATEFORMAT DMY;
SET DATEFIRST 1 -- Monday = 1

DECLARE @Date_Start AS DATETIME
DECLARE @Date_End AS DATETIME

SELECT @Date_Start = MIN ( Fromdate )
FROM TransData;
SELECT @Date_End = MAX ( Todate )
FROM TransData;

with ctedates as
(
SELECT N
, DATEADD ( d , N - 1 , @Date_Start ) AS calendar_date
, Datepart (WEEKDAY,DATEADD ( d , N - 1 , @Date_Start )) as day_of_week
,STUFF(CONVERT(CHAR(11),DATEADD(d, N-1, @Date_Start),100),4,3,'') AS MonthName
FROM Tally
WHERE (N <= DATEDIFF ( d , @Date_Start , @Date_End ))
),

cteresults as
(
SELECT fromdate,
todate,
MonthName,
event
FROM
(
SELECT td.Event1 as event , td.Fromdate, td.ToDate,c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event1 = 1) AND (c.day_of_week = 1)
UNION ALL
SELECT td.Event2 , td.Fromdate, td.ToDate,c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event2 = 1) AND (c.day_of_week = 2)
UNION ALL
SELECT td.Event3 , td.Fromdate, td.ToDate, c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event3 = 1) AND (c.day_of_week = 3)
UNION ALL
SELECT td.Event4 , td.Fromdate, td.ToDate, c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event4 = 1) AND (c.day_of_week = 4)
UNION ALL
SELECT td.Event5 , td.Fromdate, td.ToDate,c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event5 = 1) AND (c.day_of_week = 5)
UNION ALL
SELECT td.Event6 , td.Fromdate, td.ToDate, c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event6 = 1) AND (c.day_of_week = 6)
UNION ALL
SELECT td.Event7 , td.Fromdate, td.ToDate, c.MonthName
FROM
cteDates AS c INNER JOIN TransData AS td
ON c.calendar_date >= td.Fromdate
AND c.calendar_date <= td.ToDate
WHERE(td.Event7 = 1) AND (c.day_of_week = 7)
) d
)


select
fromdate,todate,
sum(case when monthname = 'apr 2013' then event else 0 end) as 'apr 2013',
sum(case when monthname = 'may 2013' then event else 0 end) as 'may 2013',
sum(case when monthname = 'jun 2013' then event else 0 end) as 'jun 2013',
sum(case when monthname = 'jul 2013' then event else 0 end) as 'jul 2013',
sum(case when monthname = 'aug 2013' then event else 0 end) as 'aug 2013',
sum(case when monthname = 'sep 2013' then event else 0 end) as 'sep 2013',
sum(case when monthname = 'oct 2013' then event else 0 end) as 'oct 2013',
sum(case when monthname = 'nov 2013' then event else 0 end) as 'nov 2013',
sum(case when monthname = 'dec 2013' then event else 0 end) as 'dec 2013',
sum(case when monthname = 'jan 2014' then event else 0 end) as 'jan 2014',
sum(case when monthname = 'feb 2014' then event else 0 end) as 'feb 2014',
sum(case when monthname = 'mar 2014' then event else 0 end) as 'mar 2014'
from cteresults
GROUP BY
fromdate, todate
ORDER BY
fromdate, todate










__________________________________________________________________
you can lead a user to data....but you cannot make them think !
__________________________________________________________________
Post #1461339
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse