我有两个表,我正在尝试使用游标将数据从两个表中获取到一个临时表中。
Tg和WTg是代表相同事物的两个不同系统的不同标签。因此,您可以将Tg视为“洗衣篮”,而WTg则是“洗衣篮”。目标是比较两个表的AVG值。我正在使用游标,因为这是查询被转储到我身上的方式。由于我们正在使用的表的大小(150,000+独特的Wtg / Tg,每个数值为100的数千个),我认为光标会更好,更快。
我对这种方法没有特别的情感依赖,所以如果你有建议,请随时纠正我。
我的查询中的一段内容为:
FETCH NEXT FROM tgListcur INTO @TgName, @W_TgName
WHILE @@fetch_status = 0
BEGIN
Insert into #Results (WTg, W_AvgVal)
select TgName, AVG(Value) as Average
from SQLTABLE1
where TgName = @W_TgName
and wwRetrievalMode = 'Delta'
and wwResolution = 0
and Datetime > @StartTime
and datetime < @EndTime
and value IS NOT NULL
Group By TgName
Insert into #Results (Tg, P_AvgVal)
Select Tag, AVG(Value) as Average
from SQLTABLE2
where Tag = @TgName
and Time > @StartTime
and Time < @EndTime
Group by Tag
FETCH NEXT FROM tgListcur INTO @TgName, @W_TgName
END
它给了我:
WTg | Tg | W_AvgVal | P_AvgVal ________________________________________________________________________________ Data1 | NULL | 50 | NULL ________________________________________________________________________________ NULL | Value1 | NULL | 50 ________________________________________________________________________________ Data2 | NULL | 70 | NULL ________________________________________________________________________________ NULL | Value2 | NULL | 70 ________________________________________________________________________________ Data3 | NULL | 60 | NULL ________________________________________________________________________________ NULL | Value3 | NULL | 60 ________________________________________________________________________________ Data4 | NULL | 12 | NULL ________________________________________________________________________________
etc. etc.
我显然想要取出空值并合并两个表,如:
WTg | Tg | W_AvgVal | P_AvgVal ________________________________________________________________________________ Data1 | Value1 | 50 | 50 ________________________________________________________________________________ Data2 | Value2 | 70 | 70 ________________________________________________________________________________ Data3 | Value3 | 60 | 60 ________________________________________________________________________________
等
我是SQL新手,任何帮助都将不胜感激。谢谢。