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
それらの出力を、次の列を持つ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)
結果を保持するための一時テーブルを作成することから始めました。最初のクエリの結果を正常に挿入できますが、2番目のクエリでテーブルを更新し、パーセンテージ列を計算する方法もわかりません。どうすればこれを機能させることができますか?