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
 General SQL Server Forums
 New to SQL Server Programming
 Select repeat events based on startdate & enddate

Author  Topic 

ram1986
Starting Member

6 Posts

Posted - 2014-11-04 : 12:01:33
Friends,
I am very new for SQL
I have one table,

Table1:
EventId EventName EventStartDate EventEndDate RepeatCount StartTime EndTime
1 Event1 2014-11-04 00:00:00.000 2014-11-25 00:00:00.000 4 4:30 PM 6:30 PM
2 Event2 2014-11-05 00:00:00.000 2014-11-12 00:00:00.000 2 3.00 AM 4.00 AM
3 Event3 2014-11-01 00:00:00.000 2014-11-15 00:00:00.000 3 1.00 PM 2.00 PM
4 Event4 2014-10-30 00:00:00.000 2014-11-06 00:00:00.000 2 5.30 PM 6.30 PM
5 Event5 2014-10-16 00:00:00.000 2014-11-16 00:00:00.000 0 5.30 PM 6.30 PM



I want to display 2 weeks Events from Table 1 depends on repeat count
(if event scheduled on Tuesday(2014-11-04) and repeat count is 4 then it should repeat the 2 row of data)

we have to display only upcomming events, if the repeat count is 0 and the startdate and enddate falls by todays date then
it should display as a single row.Data's Should be ordered by recent date

Expected Result

S.No EventId EventName EventDateTime
1 1 Event1 2014-11-04 00:00:00.000
2 2 Event2 2014-11-05 00:00:00.000
3 4 Event4 2014-11-06 00:00:00.000
4 3 Event3 2014-11-08 00:00:00.000
5 1 Event1 2014-11-11 00:00:00.000
6 2 Event2 2014-11-12 00:00:00.000
7 3 Event3 2014-11-15 00:00:00.000
8 5 Event5 2014-11-16 00:00:00.000

Assume today's date 2014-11-04 00:00:00.000 and time now 3.40 PM

ram1986
Starting Member

6 Posts

Posted - 2014-11-05 : 01:36:45
Hi Friends,
I am still waiting for your response.
Please let me know do you want any other details.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-11-05 : 03:08:55
quote:
(if event scheduled on Tuesday(2014-11-04) and repeat count is 4 then it should repeat the 2 row of data)

Repeat Count is 4, why 2 row ? and for the repeat row, the date should be adding 7 days is it ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ram1986
Starting Member

6 Posts

Posted - 2014-11-05 : 05:09:54
Hi Khtan,

Only 2 weeks events should be displayed.So for repeat count 4 , 2 rows..
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-11-05 : 07:29:00
I do not understand the data for Event5 but I suspect you could use a number/tally table. (Google this.)
The number table below is inline.

-- *** Test Data in Comsumable Format ***
-- Please provide this in future
CREATE TABLE #t
(
EventId int NOT NULL
,EventName varchar(20) NOT NULL
,EventStartDate datetime NOT NULL
,EventEndDate datetime NOT NULL
,RepeatCount int NOT NULL
,StartTime varchar(10) NOT NULL
,EndTime varchar(10) NOT NULL
);
INSERT INTO #t
SELECT 1, 'Event1','20141104','20141125', 4, '4:30 PM', '6:30 PM'
UNION ALL SELECT 2, 'Event2','20141105','20141112', 2, '3.00 AM', '4.00 AM'
UNION ALL SELECT 3, 'Event3','20141101','20141115', 3, '1.00 PM', '2.00 PM'
UNION ALL SELECT 4, 'Event4','20141030','20141106', 2, '5.30 PM', '6.30 PM'
UNION ALL SELECT 5, 'Event5','20141016','20141116', 0, '5.30 PM', '6.30 PM';
-- *** End Test Data ***

DECLARE @Now datetime;
SET @Now = '20141104 15:40';

WITH N1(N)
AS
(
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
, N2(N) AS (SELECT 1 FROM N1 A, N1 B)
, N(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1 FROM N2)
, AllEvents
AS
(
SELECT EventId, EventName
,DATEADD(week, N.N, EventStartDate) + REPLACE(StartTime, '.', ':') AS EventStart
,DATEADD(week, N.N, EventStartDate) + REPLACE(EndTime, '.', ':') AS EventEnd
FROM #t T
JOIN N
-- RepeatCount does not make sense for Event5
--ON T.RepeatCount >= N.N
ON DATEDIFF(wk, EventStartDate, EventEndDate) + 1 >= N.N
)
SELECT
ROW_NUMBER() OVER (ORDER BY EventStart) AS SNo
,EventId, EventName
,DATEADD(day, DATEDIFF(day, 0, EventStart), 0) As EventDate
FROM AllEvents
WHERE EventStart <= DATEADD(week, 2, @Now)
AND EventEnd >= @Now;

Go to Top of Page

ram1986
Starting Member

6 Posts

Posted - 2014-11-05 : 23:59:23
Sorry IFor,

I have wrongly entered the sample value for Event5 , please find the corrected value below

5 Event5 2014-11-16 00:00:00.000 2014-11-16 00:00:00.000 0 5.30 PM 6.30 PM

Please modify the procedure accordingly it will much appreciated.

your time and effort is much appreciated. please
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-11-06 : 02:24:22
quote:
Originally posted by ram1986

Hi Khtan,

Only 2 weeks events should be displayed.So for repeat count 4 , 2 rows..



You mean repeat count = 4 means 2 weeks ? and hence 2 rows ?

How do you get from 4 to 2 ? 4 / 2 = 2 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dhinasql
Posting Yak Master

195 Posts

Posted - 2014-11-06 : 03:00:11
quote:
Originally posted by khtan

quote:
Originally posted by ram1986

Hi Khtan,

Only 2 weeks events should be displayed.So for repeat count 4 , 2 rows..



You mean repeat count = 4 means 2 weeks ? and hence 2 rows ?

How do you get from 4 to 2 ? 4 / 2 = 2 ?


KH
[spoiler]Time is always against us[/spoiler]





It seems we have to fetch last two weeks record, So he exaplined the example as if there are 4 weeks record for a particular event also it should display only 2 weeks record.

In simple do not worry about the number of repeat, Just need to display most recent two weeks record.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-11-06 : 03:07:30
ah i c. thanks dhinasql


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -