English 中文(简体)
特定时间间隔两栏之间的差异
原标题:Difference between two columns for a specific time interval

i 服务器中有3栏。 第1号是日期,第2版和第2版都是浮动的。 试图缩小第2栏和第3栏之间特定时间间隔之间的差别,即每15分钟。

我找到了以下例子:

SELECT * FROM dbo.data
WHERE CAST(DateTime AS TIME) BETWEEN  14:00  and  14:30 

但是,现在无法确定如何从上述查询结果中缩小第2栏和第3栏之间的差别。 我更愿意在t-sql中这样做,除非有人相信在c#中这样做更合适。

成就

EDIT

2010-01-04 14:30:00.0000000 1.44377 1.44386
2010-01-04 14:30:00.0000000 1.4438  1.44389
2010-01-04 14:30:00.0000000 1.44377 1.44386
2010-01-05 14:00:01.0000000 1.44258 1.44267

成果如下:

0.00009  ie 1.44386 - 1.44377
0.00009     1.44389 - 1.4438
0.00009     1.44386 - 1.44377
0.00009     1.44267 - 1.44258
最佳回答

如果你将日期栏列上<代码>。 TPAS 你用多个日期结束,我认为这并不是你的意图(见下面第一点点的结果,以说明我的含义)。

DECLARE @Date DATETIME
SELECT @Date =  05/15/2012 14:15 
SELECT CAST(@Date AS TIME)
SELECT @Date =  04/15/2012 14:15 
SELECT CAST(@Date AS TIME)

我认为,你想要的是:

SELECT
    DATEADD(MINUTE, 15 * (DATEDIFF(MINUTE, 0, [Date]) / 15), 0), -- Assuming [Date] is the DATETIME column, this will give you 15 minute intervals
    AVG([Col2] - [Col3])
FROM
    dbo.data
GROUP BY
    DATEADD(MINUTE, 15 * (DATEDIFF(MINUTE, 0, [Date]) / 15), 0)

If you want to search a specific time frame, just force it in your WHERE clause:

SELECT
    AVG([Col2] - [Col3])
FROM
    dbo.data
WHERE
    [Date] BETWEEN  05/15/2012 14:00  AND  05/15/2012 14:30 
问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签