Common_table_exprs 使用交错 Lag() 函数无法引用 Select/update 中的列
Common_table_exprs with interlaced Lag() function not able to reference column in Select/update
我编写了以下查询来修复一些错误记录的数据
通常以 10 分钟的间隔记录时间戳(唯一标识符)
您可能可以跳过查询解释并跳转到查询/错误
然而,一些时间戳记录在时间戳处,例如 11 或 16 分钟
有时在 10 分钟之间会有更多条目。 interval , 以正确的 10 分钟时间或 10 分钟间隔之间的几个时间间隔
示例数据库
plantid Value Timestamp
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:01:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.21000000238418 2013-10-26 06:01:10.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:02:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:05:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:50:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:52:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:56:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 10:20:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 10:36:00.000
示例数据库解释
第 1 个正确条目
Block 2 no 10 min 值更新为 06:00 并删除其他值
块 3 正确值 8:50 删除其他两个条目
第4块10:20正确更新10:36到10:30
正如您在上面的 Table 中看到的,查询的原因是 delete/update 时间戳值
在示例数据库的第二段中,更新查询停止,因为更新第一个值后,第二个值无法更新为相同的时间戳
这就是为什么我添加了另一个导致我的问题的 where 子句(查询后解释)
查询
DECLARE @deviceId UNIQUEIDENTIFIER = '1F617EDE-7B11-416A-B8D1-FDD566020E13';
with T1 as(
Select
plantid,
LAG([S2].timestamp) over (order by [S2].timestamp) previosTime,
[S2].timestamp,
Dateadd(minute,-DATEPART(MINUTE, [s2].[timestamp])%10,[s2].[timestamp]) as newtimestamp
from [LocationWN_MNC1VE].[dbo].[T_WTG] [S2]
where [S2].[PlantID] = @deviceId
and [s2].LogInterval = 10
), T2 as(
SELECT plantid,previosTime, [s].[Timestamp], newtimestamp
FROM T1 [s]
WHERE [s].[PlantID] = @deviceId
and DATEPART(MINUTE, Timestamp) <> 00
and DATEPART(MINUTE, Timestamp) <> 10
and DATEPART(MINUTE, Timestamp) <> 20
and DATEPART(MINUTE, Timestamp) <> 30
and DATEPART(MINUTE, Timestamp) <> 40
and DATEPART(MINUTE, Timestamp) <> 50
)--Select * from T2;
-- #### delete data where original timestamp is already present ####
--Delete [LocationWN_MNC1VE].[dbo].[T_WTG]
--from [LocationWN_MNC1VE].[dbo].[T_WTG] k
--right join T2 on t2.timestamp = k.Timestamp and t2.plantid = K.PlantID
--where k.plantid = '1F617EDE-7B11-416A-B8D1-FDD566020E13' and t2.previosTime = t2.newtimestamp
-- ####test select for delete/update####
Select k.plantid, k.timestamp, t2.timestamp, t2.previosTime, t2.newtimestamp,
LAG([K].timestamp) over (order by [K].timestamp) previosTimeNewSelect
from [LocationWN_MNC1VE].[dbo].[T_WTG] as k
right join T2 on t2.timestamp = k.Timestamp and t2.plantid = K.PlantID
where k.plantid = @deviceId and t2.previosTime <> t2.newtimestamp and t2.newtimestamp <> previosTimeNewSelect
-- #### update the data ####
--update [LocationWN_MNC1VE].[dbo].[T_WTG]
--SET timestamp = T2.newtimestamp
--FROM [LocationWN_MNC1VE].[dbo].[T_WTG] [f]
--right join T2 ON T2.timestamp = F.Timestamp and t2.plantid = f.PlantID
--where f.plantid = '1F617EDE-7B11-416A-B8D1-FDD566020E13' and t2.previosTime <> t2.newtimestamp
错误
添加 where 语句时 "and t2.newtimestamp <> previosTimeNewSelect"
为了能够识别加倍的数据集,我得到以下错误
Msg 207, Level 16, State 1, Line 32
Invalid column name 'previosTimeNewSelect'.
我已经用 k 和 T2 尝试了每个选项
请让我知道我的错误在哪里
where 子句在逻辑上先于 select 子句求值,因此 previosTimeNewSelect
在那里不可用。
您需要在派生的 table 或另一个 CTE 中计算 previosTimeNewSelect
的值,然后在外部查询的 where 子句中过滤列。
这是您尝试执行的操作的简化版本。
declare @T table(ID int identity);
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
where LID = 1;
结果:
Msg 207, Level 16, State 1, Line 7
Invalid column name 'LID'.
改用派生的 table
select TT.ID,
TT.LID
from (
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
) as TT
where TT.LID = 1;
或 CTE
with TT as
(
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
)
select TT.ID,
TT.LID
from TT
where TT.LID = 1
我编写了以下查询来修复一些错误记录的数据 通常以 10 分钟的间隔记录时间戳(唯一标识符)
您可能可以跳过查询解释并跳转到查询/错误
然而,一些时间戳记录在时间戳处,例如 11 或 16 分钟 有时在 10 分钟之间会有更多条目。 interval , 以正确的 10 分钟时间或 10 分钟间隔之间的几个时间间隔
示例数据库
plantid Value Timestamp
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:01:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.21000000238418 2013-10-26 06:01:10.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:02:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 1.10000002384186 2013-10-26 06:05:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:50:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:52:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 08:56:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 10:20:00.000
1F617EDE-7B11-416A-B8D1-FDD566020E13 0.800000011920929 2013-10-26 10:36:00.000
示例数据库解释
第 1 个正确条目
Block 2 no 10 min 值更新为 06:00 并删除其他值
块 3 正确值 8:50 删除其他两个条目
第4块10:20正确更新10:36到10:30
正如您在上面的 Table 中看到的,查询的原因是 delete/update 时间戳值 在示例数据库的第二段中,更新查询停止,因为更新第一个值后,第二个值无法更新为相同的时间戳 这就是为什么我添加了另一个导致我的问题的 where 子句(查询后解释)
查询
DECLARE @deviceId UNIQUEIDENTIFIER = '1F617EDE-7B11-416A-B8D1-FDD566020E13';
with T1 as(
Select
plantid,
LAG([S2].timestamp) over (order by [S2].timestamp) previosTime,
[S2].timestamp,
Dateadd(minute,-DATEPART(MINUTE, [s2].[timestamp])%10,[s2].[timestamp]) as newtimestamp
from [LocationWN_MNC1VE].[dbo].[T_WTG] [S2]
where [S2].[PlantID] = @deviceId
and [s2].LogInterval = 10
), T2 as(
SELECT plantid,previosTime, [s].[Timestamp], newtimestamp
FROM T1 [s]
WHERE [s].[PlantID] = @deviceId
and DATEPART(MINUTE, Timestamp) <> 00
and DATEPART(MINUTE, Timestamp) <> 10
and DATEPART(MINUTE, Timestamp) <> 20
and DATEPART(MINUTE, Timestamp) <> 30
and DATEPART(MINUTE, Timestamp) <> 40
and DATEPART(MINUTE, Timestamp) <> 50
)--Select * from T2;
-- #### delete data where original timestamp is already present ####
--Delete [LocationWN_MNC1VE].[dbo].[T_WTG]
--from [LocationWN_MNC1VE].[dbo].[T_WTG] k
--right join T2 on t2.timestamp = k.Timestamp and t2.plantid = K.PlantID
--where k.plantid = '1F617EDE-7B11-416A-B8D1-FDD566020E13' and t2.previosTime = t2.newtimestamp
-- ####test select for delete/update####
Select k.plantid, k.timestamp, t2.timestamp, t2.previosTime, t2.newtimestamp,
LAG([K].timestamp) over (order by [K].timestamp) previosTimeNewSelect
from [LocationWN_MNC1VE].[dbo].[T_WTG] as k
right join T2 on t2.timestamp = k.Timestamp and t2.plantid = K.PlantID
where k.plantid = @deviceId and t2.previosTime <> t2.newtimestamp and t2.newtimestamp <> previosTimeNewSelect
-- #### update the data ####
--update [LocationWN_MNC1VE].[dbo].[T_WTG]
--SET timestamp = T2.newtimestamp
--FROM [LocationWN_MNC1VE].[dbo].[T_WTG] [f]
--right join T2 ON T2.timestamp = F.Timestamp and t2.plantid = f.PlantID
--where f.plantid = '1F617EDE-7B11-416A-B8D1-FDD566020E13' and t2.previosTime <> t2.newtimestamp
错误 添加 where 语句时 "and t2.newtimestamp <> previosTimeNewSelect" 为了能够识别加倍的数据集,我得到以下错误
Msg 207, Level 16, State 1, Line 32 Invalid column name 'previosTimeNewSelect'.
我已经用 k 和 T2 尝试了每个选项
请让我知道我的错误在哪里
where 子句在逻辑上先于 select 子句求值,因此 previosTimeNewSelect
在那里不可用。
您需要在派生的 table 或另一个 CTE 中计算 previosTimeNewSelect
的值,然后在外部查询的 where 子句中过滤列。
这是您尝试执行的操作的简化版本。
declare @T table(ID int identity);
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
where LID = 1;
结果:
Msg 207, Level 16, State 1, Line 7
Invalid column name 'LID'.
改用派生的 table
select TT.ID,
TT.LID
from (
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
) as TT
where TT.LID = 1;
或 CTE
with TT as
(
select T.ID,
lag(T.ID) over (order by T.ID) as LID
from @T as T
)
select TT.ID,
TT.LID
from TT
where TT.LID = 1