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

それらの出力を、次の列を持つ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番目のクエリでテーブルを更新し、パーセンテージ列を計算する方法もわかりません。どうすればこれを機能させることができますか?

4

3 に答える 3

2

これは、1つのクエリを使用して実行できます。以下を参照してください。

;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

2つのクエリからビューを作成し、ビューに挿入できます。つまり、基本的に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 に答える
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 に答える