Sql 使用2个查询的结果构造一个表

Sql 使用2个查询的结果构造一个表,sql,sql-server,sql-update,Sql,Sql Server,Sql Update,我有两个问题: 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

我有两个问题:

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

我想将它们的输出合并到一个包含以下列的结果表中:


我已经开始创建一个临时表来保存结果,并且我可以成功地插入第一个查询的结果,但是我不知道如何使用第二个查询更新表并计算百分比列。如何使其工作?

您可以从这两个查询中创建一个视图并插入到视图中。 所以基本上你需要创建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 ... 

希望有帮助

假设您分别在
@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
),因此请注意这一点,并随意修改名称。

您可以使用一个查询完成此操作,请参见下文

;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

哇,谢谢。我只是稍微调整了一下,这样百分比计算会返回小数而不是零,一切都很好。你是救命恩人。
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
;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