合并两个SQL Server查询

时间:2015-02-03 06:50:06

标签: sql sql-server sql-server-2008

如何合并这两个查询?

i)

SELECT 
      runId,
      Runs.prodId,
      prodDate,
      prodName,
      buildNumber,
      totalCount as TotalTestCases,
      (passCount*100)/(passCount+failCount) as PassPercent, 
      passCount,
      failCount,
      runOwner 
FROM Runs,Product 
WHERE Runs.prodId=Product.prodId

ii)

SELECT (CAST(counts.Count as decimal(10,4)) / CAST(failCount as decimal(10,4))) as PercentAnalysed
FROM Runs 
LEFT JOIN 
(SELECT 
    COUNT(*) AS 'Count', 
    runId
 FROM Results WHERE Analysed = 'True' GROUP BY runId 
)counts on counts.runId = Runs.runId

我试过了:

SELECT 
      Runs.runId,
      Runs.prodId,
      prodDate,prodName,
      buildNumber,
      totalCount as TotalTestCases,
      (passCount*100)/(passCount+failCount) as PassPercent, 
      passCount,
      failCount,
      runOwner,
      counts.runId,
      (cast(counts.Count as decimal(10,4)) / cast(failCount as decimal(10,4))) as PercentAnalysed 
FROM Runs,Product 
LEFT JOIN 
(SELECT COUNT(*) AS 'Count', 
    runId 
 FROM Results WHERE Analysed = 'True' GROUP BY runId 
) counts on counts.runId = Runs.runId
WHERE Runs.prodId=Product.prodId

但它会出错。

单独地,两个查询都运行正常。此外,两个查询返回的行数相同,因此不是问题。

错误是:

  

"消息4104,级别16,状态1,行13多部分标识符   " Runs.runId"无法受约束。"

3 个答案:

答案 0 :(得分:1)

使用Inner Join加入运行和产品表。

select Runs.runId,
Runs.prodId,
prodDate,
prodName,
buildNumber,
totalCount as TotalTestCases,
(passCount*100)/(passCount+failCount) as PassPercent, 
passCount,
failCount,
runOwner,
counts.runId,
(cast(counts.Count as decimal(10,4)) / cast(failCount as decimal(10,4))) as PercentAnalysed 
from 
Runs AS Runs Inner Join Product AS Product On Runs.prodId=Product.prodId
left join 
( 
    SELECT COUNT(*) AS 'Count', runId FROM Results WHERE Analysed = 'True' GROUP BY runId 
) counts on counts.runId = Runs.runId

答案 1 :(得分:0)

如果你的两个第一个查询工作正常,那么你可以在下面这样的查询中使用它们,它应该正确地说出来(你只需要一个连接条件)

select * from --select columns you want
-- Query i
(
  select runId,Runs.prodId,prodDate,prodName,buildNumber,totalCount as
    TotalTestCases,(passCount*100)/(passCount+failCount) as PassPercent, 
    passCount,failCount,runOwner from Runs,Product where 
    Runs.prodId=Product.prodId
) qi --you need to have a join column inside it i.e runId
join
-- Query ii
(
select runId , (cast(counts.Count as decimal(10,4)) / cast(failCount as decimal(10,4))) as PercentAnalysed
from Runs 
left join ( SELECT COUNT(*) AS 'Count', runId FROM Results WHERE Analysed = 'True' GROUP BY runId ) counts on counts.runId = Runs.runId
) qii --you need to have a join column inside it. i.e runId

on qi.runId=qii.runId 

答案 2 :(得分:0)

你混合where和join语句。这不起作用:

select Runs.runId,Runs.prodId,prodDate,prodName,buildNumber,
    totalCount as TotalTestCases,(passCount*100)/(passCount+failCount) as PassPercent, passCount,failCount,runOwner,
    counts.runId,(cast(counts.Count as decimal(10,4)) / cast(failCount as decimal(10,4))) as PercentAnalysed 
from Runs join Product  on Runs.runId= Product.prodId
left join ( SELECT COUNT(*) AS 'Count', runId FROM Results WHERE Analysed = 'True' GROUP BY runId ) counts on counts.runId = Runs.runId

当您使用from in语句时,您必须为每个表使用join。