I have 2 queries:
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
and
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
I’d like to combine their output into one table of results with the following columns:
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)
I have started by creating a temp table to hold the results, and I can successfully insert the first query’s results, but I can’t figure out how to update the table with the second query and compute the percentage columns too. How can I get this to work?
You can do this using one query, see below