开发者

SQL Server Database query help [duplicate]

This question already has an answer here: Closed 12 years ago.

Possible Duplicate:

SQL Server Database query help

Hi,

I have a problem that I didn't manage to solve for a very long time, and I quite desperate.

I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name' (Data Type: nvarchar(50)), 'Start_At' (DataType: DateTime) and 'End_At' (Data Type: DateTime).

I need to know for each 'Time-Interval' (let's say 1 minute) how many processes (Process_Name = PN) was open (after or equal to the 'Start_at' column and before or equal to the 'End_At' column) during this time (It can be a few rows with the same data).

Does anyone know how to make this query without a 'for' loop? (It ITSELF will promote the time), 开发者_运维技巧(The answer will be a table with two columns (1. The time the check took place. 2. the number of open processes at this time.) and a row for each 'Time-Interval' (1 minute in this example)

(If it help, I'm working with C# .net)

Many thanks,


The following should return the check time and numer of processes from the table where the start time is smaller or equal to the requested point of time and the end time is greater or equal:

DECLARE @pot DATETIME

-- SET @pot TO THE POINT OF TIME YOU WANT THE DETERMINE THE ENTRIES FOR

SELECT @pos, COUNT(*) FROM ProcessData
WHERE @pot BETWEEN Start_At AND End_At

This assumes you're using SQL Server - but there may be some equivalent in other dialects.

You can extend this for time intervals also, but doing two checks, one for "start point of time" and one for "end point of time".


Take a look at these two SO questions/answers SQL Server related question and sql query to solve the following

They both deal with the same issue...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜