使用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'(OffLine)。这可能很容易跨越很多天或几周。
任何人都可以帮我吗?
更新回复: 感谢您的回复 - 您有正确的想法,但它只返回每天午夜的值,这是我自己尝试过的。我开始认为这个数据库设计是真正的问题,如果保持这种状态会让我发疯。 TagID 3004全天每5分钟记录一次。 TagID 10003仅在更改值时(以及每天午夜)记录。因此,如果它不会在数小时或数天内更改值,则我从第二个查询中得不到任何值。即使它确实如此,两者的时间戳很少匹配,因此不太可能找到结果。
要演示 - 使用此在Online位上的查询,您可以看到OnLine每天有多少条目存在。
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?
答案 0 :(得分: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之间的TIMEDD。我们已经有了Y.然后X将是最接近值0的日期。尝试上面编辑的查询并让我知道