| Author |
Topic |
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2002-12-26 : 18:54:33
|
I want a count of the number of records where the startdate /enddate range falls between a certain time.I want 1 record for every second (422 total seconds in question in this data) and the record should contain the count of the number of "members" that have a startdate / end date that occured during that second in time. I hope that makes sense.Desired OutputSecondNumber | NumMembers-------------------------------1 12 13 14 15 16 27 28 29 210 2 Here's some sample data: CREATE TABLE #member(MemberID INT, ConferenceID INT, StartDate DATETIME, EndDate DATETIME)Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('17','100285','12/26/2002 5:27:00 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('18','100285','12/26/2002 5:27:06 PM','12/26/2002 5:33:06 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('19','100285','12/26/2002 5:27:18 PM','12/26/2002 5:33:12 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('20','100285','12/26/2002 5:27:20 PM','12/26/2002 5:33:23 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('21','100285','12/26/2002 5:31:26 PM','12/26/2002 5:33:32 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('22','100285','12/26/2002 5:32:17 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('23','100285','12/26/2002 5:32:30 PM','12/26/2002 5:33:39 PM')SELECT DATEDIFF(ss, MIN(StartDate), MAX(EndDate)) FROM #MemberDROP TABLE #memberBTW guys, this isn't a homework assignment, although it does sound like one.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-26 : 19:45:55
|
Would something like this work for you?CREATE TABLE #member(MemberID INT, ConferenceID INT, StartDate DATETIME, EndDate DATETIME)Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('17','100285','12/26/2002 5:27:00 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('18','100285','12/26/2002 5:27:06 PM','12/26/2002 5:33:06 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('19','100285','12/26/2002 5:27:18 PM','12/26/2002 5:33:12 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('20','100285','12/26/2002 5:27:20 PM','12/26/2002 5:33:23 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('21','100285','12/26/2002 5:31:26 PM','12/26/2002 5:33:32 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('22','100285','12/26/2002 5:32:17 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('23','100285','12/26/2002 5:32:30 PM','12/26/2002 5:33:39 PM')CREATE TABLE #NumRange(n int)DECLARE @MinNum intDECLARE @MaxNum intDECLARE @StartDate datetimeDECLARE @I intSET NOCOUNT ONSET @I = 0WHILE @I <= 9 BEGIN INSERT INTO #NumRange VALUES(@I) SET @I = @I + 1ENDSET @MinNum = 1SELECT @MaxNum = DATEDIFF(ss, MIN(StartDate), MAX(EndDate)) FROM #MemberSELECT @StartDate = MIN(StartDate) FROM #MemberSELECT A.Number , A.SerialDate, COUNT(*) As NumberOfMembersFROM(SELECT dateadd(ss, (a.n + (b.n * 10) + (c.n * 100) + (d.n * 1000) + (e.n * 10000)),@StartDate) As SerialDate, (a.n + (b.n * 10) + (c.n * 100) + (d.n * 1000) + (e.n * 10000)) As NumberFROM #NumRange aCROSS JOIN #NumRange bCROSS JOIN #NumRange cCROSS JOIN #NumRange dCROSS JOIN #NumRange eWHERE a.n + (b.n * 10) + (c.n * 100) + (d.n * 1000) + (e.n * 10000) BETWEEN @MinNum AND @MaxNum) A, #Member B WHERE A.SerialDate Between B.StartDate AND B.EndDateGROUP BY A.Number, SerialDateORDER BY A.NumberDROP TABLE #memberDROP TABLE #NumRangeTook the tally table idea from a post I read a while back.Edited by - ValterBorges on 12/26/2002 19:53:17Edited by - ValterBorges on 12/26/2002 19:56:12 |
 |
|
|
Peter Dutch
Posting Yak Master
127 Posts |
Posted - 2002-12-27 : 07:00:26
|
Valter, I think you found a great solution, but wouldn't this be a lot easier?CREATE TABLE #member(MemberID INT, ConferenceID INT, StartDate DATETIME, EndDate DATETIME)Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('17','100285','12/26/2002 5:27:00 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('18','100285','12/26/2002 5:27:06 PM','12/26/2002 5:33:06 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('19','100285','12/26/2002 5:27:18 PM','12/26/2002 5:33:12 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('20','100285','12/26/2002 5:27:20 PM','12/26/2002 5:33:23 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('21','100285','12/26/2002 5:31:26 PM','12/26/2002 5:33:32 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('22','100285','12/26/2002 5:32:17 PM','12/26/2002 5:34:02 PM')Insert Into #member(MemberID,ConferenceID,StartDate,EndDate) VALUES ('23','100285','12/26/2002 5:32:30 PM','12/26/2002 5:33:39 PM')DECLARE @i intDECLARE @TotalSeconds INTDECLARE @mindate DATETIMESELECT @TotalSeconds = DATEDIFF(ss, MIN(StartDate), MAX(EndDate)) FROM #MemberSELECT @MinDate = MIN(StartDate) FROM #MemberSET NOCOUNT ONCREATE TABLE #num (n int)SELECT @i = 0WHILE @i <= @TotalSeconds BEGIN INSERT #num (n) VALUES (@i) SELECT @i = @i + 1END SELECT n.n as TheSecond, (SELECT COUNT(*) FROM #member WHERE DATEDIFF(ss,@mindate, StartDate) <= n.n AND DATEDIFF(ss,@mindate, EndDate) >= n.n) as NumberOfMembersFROM #num nDROP TABLE #memberDROP TABLE #num |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-27 : 09:16:36
|
| Nice optimization Pete. |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2002-12-27 : 09:23:28
|
Wow to both solutions.I'm not sure that the initial premise was interpeted correctly. Other than that I have nothing to add. quote: count of the number of "members" that have a startdate / end date that occured during that second in time.
Has been interpeted as: Count all members whose time interval overlaps each second.Another interpetation is: "members" that have a startdate or enddate that occured during that second in time.If the latter interpetation is right, change the AND to OR in the last WHERE in Peter's solution and you have it.Sam |
 |
|
|
Peter Dutch
Posting Yak Master
127 Posts |
Posted - 2002-12-27 : 09:26:37
|
| Yes, guess you could be right.I thought he wanted to know the 'active' users per second |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2002-12-27 : 11:11:27
|
| Wow guys, GREAT work!I'm going to look at these solutions and see if they come up with teh same answer that I calculated by hand. Valter, you have almost the exact same code that I have in my "in progress" version :)Peter, very nicly done! Simple and effective!Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
|
|
|