1

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

我首先创建了一个临时表来保存结果,并且我可以成功插入第一个查询的结果,但我不知道如何使用第二个查询更新表并计算百分比列。我怎样才能让它工作?

4

3 回答 3

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
于 2012-12-06T18:14:18.190 回答
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),因此请注意这一点,并根据您的喜好调整名称。

于 2012-12-06T18:02:11.150 回答
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 ... 

希望能帮助到你!

于 2012-12-06T17:55:31.513 回答