使用来自2个查询的结果构造表

时间:2012-12-06 17:52:26

标签: sql sql-server sql-update

我有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)

我已经开始创建一个临时表来保存结果,我可以成功插入第一个查询的结果,但我无法弄清楚如何使用第二个查询更新表并计算百分比列。我怎样才能让它发挥作用?

3 个答案:

答案 0 :(得分:2)

您可以使用一个查询执行此操作,请参阅下文

;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

答案 1 :(得分:0)

您可以从两个查询创建视图并插入视图。 所以基本上你需要创建3个视图

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 ... 

希望它有所帮助!

答案 2 :(得分:0)

假设您分别在@table1@table2中获得了每个查询的结果,那么这应该适合您:

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

请注意,我使用的列名称需要与您的源数据对齐(例如percentofrepeat),因此请注意这一点并随意摆弄名称。