Click here to Skip to main content
15,911,646 members
Home / Discussions / Database
   

Database

 
QuestionWHich is more optimal?? Pin
happyheartcs27-Sep-06 23:40
happyheartcs27-Sep-06 23:40 
Questionpb connecting to remote sql server Pin
rama charan27-Sep-06 21:04
rama charan27-Sep-06 21:04 
AnswerRe: pb connecting to remote sql server Pin
Rob Graham28-Sep-06 4:57
Rob Graham28-Sep-06 4:57 
GeneralRe: pb connecting to remote sql server Pin
rama charan3-Oct-06 19:29
rama charan3-Oct-06 19:29 
QuestionBuinding in DataGridView Pin
nesaraja27-Sep-06 19:08
nesaraja27-Sep-06 19:08 
AnswerRe: Buinding in DataGridView Pin
just3ala227-Sep-06 22:39
just3ala227-Sep-06 22:39 
Questiondatabse problem Pin
With_problem27-Sep-06 18:55
With_problem27-Sep-06 18:55 
QuestionGrouping events that fall within x days of other events? Pin
T-Smooth27-Sep-06 16:34
T-Smooth27-Sep-06 16:34 
I'm having trouble coming up with a query in SQL Server 2000. Trying my best to sum it up in one sentence; I need a result set that for every EventID/StartDate that exists in tblEvents, I need all StartDates within x days to have a minimum sequential, unique number starting at 1. Now I'll go into some more detail and possibly confuse everyone.

What I have is a table of events that to simplify this question include only an EventID and an StartDate field. What I need to do is for each event, find the other events that occur within x days after the event in question. I then need to number the events starting at 1 so that each eventB that occurs within x days of EventA has the minimum number different from any events from the past x days. I know I just sucked at putting it in words so I'll give an example.

Here is what my idea of the end result table should like if x=2. The source table is basically the same thing minus the Num column.

tblEvents
------------
EventID Date Num
1 10/1/06 1
3 10/2/06 2
7 10/1/06 3
4 10/4/06 1
6 10/10/06 1
2 10/20/06 1
5 10/21/06 2


Here's a quick diagram I made (I know it's ugly and not to scale) which hopefully helps you understand what I need better. Basically, I will be drawing a timeline using vml and each item is going to have text next to it so I need items to be staggered if they are on the same day or close to each other so that things don't overlap. I also need to minimize the vertical space it takes up which is why num needs to get back to 1 as soon as it can instead of just putting each event on its own line. The "num" column will tell me what row to put the item in.

http://i10.tinypic.com/43pxycj.gif

The following query gets me closer I think but it has a few problems and could be the wrong approach completely. I need this to work in SQL Server 2000 and I know that Row_Number and also Over() I think are both SQL Server 2005 only which is one problem with what I've come up with so far. Also, the partition I use has a separate value for the mirror of items (eg. EventID 7 should not be on row 1). Any tips or thoughts would be appreciated.



SELECT E1.EventID AS E1_EventID, E1.StartDate AS E1_StartDate, E2.EventID AS E2_EventID, E2.StartDate AS E2_StartDate, DATEDIFF(day, E2.StartDate, E1.StartDate) As Diff, ROW_NUMBER() OVER(PARTITION BY E1.EventID ORDER BY E1.StartDate, E1.EventID) FROM tblEvents AS E1 LEFT OUTER JOIN tblEvents AS E2 ON E1.EventID < E2.EventID AND ABS(DATEDIFF(day, E1.StartDate, E2.StartDate)) <= 2

Thanks for any help!
Tom
QuestionHow to Select the last N records based on date? Pin
shapper27-Sep-06 15:42
shapper27-Sep-06 15:42 
AnswerRe: How to Select the last N records based on date? Pin
nguyenvhn27-Sep-06 16:00
nguyenvhn27-Sep-06 16:00 
GeneralRe: How to Select the last N records based on date? Pin
shapper27-Sep-06 16:52
shapper27-Sep-06 16:52 
GeneralRe: How to Select the last N records based on date? Pin
nguyenvhn28-Sep-06 0:23
nguyenvhn28-Sep-06 0:23 
QuestionSQL Server Reporting Services ReportViewer Pin
catmom27-Sep-06 11:46
catmom27-Sep-06 11:46 
Questiongetting information based on DateTime Pin
ONeil Tomlinson27-Sep-06 10:39
ONeil Tomlinson27-Sep-06 10:39 
AnswerRe: getting information based on DateTime Pin
nguyenvhn27-Sep-06 16:05
nguyenvhn27-Sep-06 16:05 
QuestionUpdate DB from DataGridView Pin
anderslundsgard27-Sep-06 10:22
anderslundsgard27-Sep-06 10:22 
QuestionRestore "AdventureWorks" database Pin
VK-Cadec27-Sep-06 9:53
VK-Cadec27-Sep-06 9:53 
AnswerRe: Restore "AdventureWorks" database Pin
Chris Buckett28-Sep-06 23:35
Chris Buckett28-Sep-06 23:35 
QuestionExecuting a Stored Procedure thru VS2005 Pin
Brendan Vogt27-Sep-06 4:25
Brendan Vogt27-Sep-06 4:25 
AnswerRe: Executing a Stored Procedure thru VS2005 Pin
nguyenvhn27-Sep-06 16:09
nguyenvhn27-Sep-06 16:09 
QuestionError: Attempt to open a table failed - there were no columns to retrieve were specified. Pin
AbbyDabby27-Sep-06 4:19
AbbyDabby27-Sep-06 4:19 
AnswerRe: Error: Attempt to open a table failed - there were no columns to retrieve were specified. Pin
Kschuler27-Sep-06 8:34
Kschuler27-Sep-06 8:34 
QuestionComplex SQL View Pin
Dayekh27-Sep-06 1:45
Dayekh27-Sep-06 1:45 
AnswerRe: Complex SQL View Pin
albCode27-Sep-06 2:18
albCode27-Sep-06 2:18 
GeneralRe: Complex SQL View Pin
Dayekh27-Sep-06 3:06
Dayekh27-Sep-06 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.