我有 2 个查询:
SELECT p.assetid,
p.TagId,
SUM(CASE WHEN p.isrepeat = 1 then 1 else 0 END) as 'Repeats',
SUM(CASE WHEN p.isrepeat = 0 then 1 else 0 END) as 'Non-Repeats',
CAST(SUM(CASE WHEN p.isrepeat =1 then 1 else 0 END) as DECIMAL)/COUNT(*)as 'Percent of Repeats'
from POSITION p
group by p.tagid, p.assetid
order by 1
和
SELECT p.AssetID, p.tagid, COUNT(*)
from POSITION p,
TEMP t
where t.beginning_X = p.X
and t.beginning_Y = p.y
and p.isrepeat = 1
and t.AssetID = p.AssetID
and t.Total_Distance_Traveled > 1
group by p.AssetID, p.tagid
order by 1
我想将他们的输出合并到一个包含以下列的结果表中:
AssetID, TagID, Repeats (from the first query), Non-Repeats (from the first query), % of Repeats (from the first query), Calc1 (difference of repeats in first query and count result from second query, grouped by asset id), Calc1% (Calc1 result/repeats from the first query, grouped by assetid), Calc2 (count result from the second query, grouped by assetid) Cacl2%(Calc2 result/repeats from the first query, grouped by assetid)
我首先创建了一个临时表来保存结果,并且我可以成功插入第一个查询的结果,但我不知道如何使用第二个查询更新表并计算百分比列。我怎样才能让它工作?