Reputation: 167
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?
Upvotes: 1
Views: 63
Reputation: 27437
You can do this using one query, see below
;WITH CTE AS (
SELECT
--q1
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',
--q2
COUNT(t.AssetID) CNT2,
from POSITION p
LEFT OUTER JOIN TEMP t ON t.beginning_X = p.X
and t.beginning_Y = p.y and t.AssetID = p.AssetID
AND p.isrepeat = 1 and t.Total_Distance_Traveled > 1
group by p.tagid, p.assetid
)
SELECT
Assetid, TagId, Repeats, Non-Repeats, [Percent of Repeats],
(repeats - cnt2) calc1, ((repeats - cnt2)/repeats [Per calc1],
(cnt2) calc2, (cnt2/repeats) [Per calc2]
FROM CTE
Upvotes: 2
Reputation: 385
You can create a view from the two queries and insert into the view. So basically you need to create 3 views
create view v1 as
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
create view v2 as
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
create view v3 as
select AssetID, TagID, Repeats, Non-Repeats, Percent of Repeats,calc1(do ur cal), calc2, calc2% from v1, v2 where ...
Hope it helps!
Upvotes: 0
Reputation: 8994
Assuming you have the results of each of those queries in @table1
and @table2
respectively, then this should do it for you:
SELECT t1.AssetID, t1.TagID, t1.Repeats, t1.nonrepeats, t1.percentofrepeats,
(t1.repeats - t2.count) AS 'Calc1',
((t1.repeats - t2.count) / t1.repeats) AS 'Calc1%',
t2.count AS 'Calc2',
(t2.count / t1.results) AS 'Calc2%'
FROM @table1 t1
JOIN @table2 t2 ON t1.assetid = t2.assetid AND t1.tagid = t2.tagid
Note that the column names I use need to line up with your source data (for example percentofrepeat
), so pay attention to that and fiddle with names as you please.
Upvotes: 0