0

使用 SQL Server 2012

我可以提出这两个查询来获得我需要的两个部分,但我不知道如何组合它们并获得一个结果表。

--Returns Max Value of instrument per day
Declare @StDate DateTime
Declare @EndDate DateTime
Set @STDate = '11/1/2012'
Set @EndDate = '12/1/2012'
Select dateadd(day,(datediff(day,0,TIMEandDATE)),0) as MyDate, MAX(Value) AS Max_Turb 
From Turbs where TagID = 3004
and TimeAndDate >= @StDate
and TimeAndDate < @EndDate
Group by dateadd(day,(datediff(day,0,TIMEandDATE)),0)
Order by dateadd(day,(datediff(day,0,TIMEandDATE)),0)

--returns when the process line status bit changes to On_Line
Declare @StDate DateTime
Declare @EndDate DateTime
Set @STDate = '11/1/2012'
Set @EndDate = '12/1/2012'
SELECT TIMEandDATE as MyDate, Value as On_Line
  FROM StatusBits
  Where TagID = 10003
  and Value = 1
  and TimeAndDate >= @StDate
and TimeAndDate < @EndDate

TagID 10003 不会全天填充,它仅在状态更改时被记录并加盖时间戳。

TagID 3004 整天都在记录,不考虑其他任何事情。因此,它将记录无意义的值。TagID 10003 位表示进程正在运行,这些值是有效的,应该考虑输出。TagID 10003 位是与许多不同进程相关的全局指示,并且对于这个值不是唯一的。

我正在寻找的结果需要每天显示 TagID 3004 的 MAX 值(来自表 Turbs),但仅在 TagID 10003 变为“1”(在线)直到变为“0”(离线)。这很容易跨越数天或数周。

谁能帮我这个?

更新回复:感谢您的回复-您的想法是正确的,但它仅返回每天午夜的值,这是我自己尝试所看到的。我开始认为这种数据库设计是真正的问题,如果保持这种方式会让我发疯。TagID 3004 全天每 5 分钟记录一次。TagID 10003 仅在更改值时(以及每天午夜)才会被记录。因此,如果它在几个小时或几天内没有改变值,我从第二个查询中没有得到任何值。并且即使这样做,两者的时间戳也很少匹配,因此不太可能找到结果。

为了演示 - 通过在线位上的此查询,您可以看到在线在线每天存在的条目数。

DECLARE @OnLine TABLE (RN int IDENTITY(1,1), TIMEandDATE datetime, VALUE float)
Declare @StartDT DateTime
Declare @EndDT DateTime
Set @StartDT = '11/1/2012'
Set @EndDT = '12/1/2012'

INSERT INTO @OnLine
               SELECT TOP 1 @StartDT, 
        VALUE FROM StatusBits 
        WHERE TagID = 10003 
        AND TIMEandDATE < @StartDT 
        ORDER BY TIMEandDATE DESC
INSERT INTO @OnLine
               SELECT TIMEandDATE, 
        VALUE FROM StatusBits 
        WHERE TagID = 10003
                AND TIMEandDATE >= @StartDT 
        AND TIMEandDATE < @EndDT
               ORDER BY TIMEandDATE
INSERT INTO @OnLine
               SELECT TOP 1 @ENDDT, 
        VALUE FROM StatusBits 
        WHERE TagID = 10003 
        AND TIMEandDATE < @EndDT 
        ORDER BY TIMEandDATE DESC

返回以下内容:

record  Timestamp       Value
1   2012-11-01 00:00:00.000 0
2   2012-11-01 23:59:59.087 0
3   2012-11-02 23:11:44.450 1
4   2012-11-03 00:00:00.807 1
5   2012-11-03 13:47:26.113 0
6   2012-11-03 13:52:42.083 1
7   2012-11-03 14:24:54.287 0
8   2012-11-03 23:59:59.387 0
9   2012-11-04 23:34:16.257 1
10  2012-11-04 23:59:59.840 1
11  2012-11-05 15:31:07.133 1
12  2012-11-05 23:59:59.230 1
13  2012-11-06 07:47:37.477 0
14  2012-11-06 15:44:48.247 0
15  2012-11-06 23:59:59.013 0
16  2012-11-07 07:11:33.280 1
17  2012-11-08 00:00:00.537 1 

我试图在 TagID 10003 从 0 变为 1 之间的时间内返回 tagID 3004 的结果。我看不出这是如何查询的。也许以某种方式使用 BETWEEN ?

4

1 回答 1

0

您可以使用它来选择第二个表中的值:

Declare @StDate DateTime = '11/1/2012'
Declare @EndDate DateTime = '12/1/2012'

;WITH
OnlineDates AS
(
    SELECT TIMEandDATE as MyDate
    FROM StatusBits
    Where TagID = 10003
        and Value = 1
        and TimeAndDate >= @StDate
        and TimeAndDate < @EndDate
)
Select dateadd(day,(datediff(day,0,TIMEandDATE)),0) as MyDate, MAX(Value) AS Max_Turb 
From Turbs T 
    INNER JOIN
        OnlineDates OD
    ON 
        t.TIMEandDATE BETWEEN 
        (
            SELECT TOP (1) X.TIMEandDATE
            FROM StatusBits X
            WHERE X.TagID = 10003
                AND X.Value = 0
                AND X.TIMEandDATE <= t.TIMEandDATE
            ORDER BY DESC

        )
        AND OD.MyDate
where TagID = 3004
Group by dateadd(day,(datediff(day,0,TIMEandDATE)),0)
Order by dateadd(day,(datediff(day,0,TIMEandDATE)),0)

但是,不确定您是否想要从第二个表中选择最大和最小日期,然后按此过滤。

希望这可以帮助!

编辑:为了获得您要查找的内容,您需要 X 和 Y 之间的时间和日期。我们已经有了 Y。那么 X 将是值为 0 的最接近的日期。尝试上面编辑的查询并告诉我。

于 2013-02-05T21:31:20.557 回答