求一个给出错误的子查询

时间:2015-07-20 16:30:56

标签: sql sql-server-2008

我查看了其他标题,可以看到可能需要的一些内容,但无法使其发挥作用。这是我的询问......

SELECT s.RepairCode

,sum((SELECT isnull (Sum(Convert(numeric(8,2), ((tp2.FootWeight/12)*(FinishLength)/2000))),0)
FROM (NYS2Reheat r2 INNER JOIN NYS2SawPieces s2 ON r2.recordId = s2.rollrecID)
left join TensileProducts tp2 on r2.FinalProd = tp2.sqlproduct
where (s2.recordid = s.recordid) and tp2.Active = 1)) as tons

FROM (NYS2Reheat r INNER JOIN NYS2SawPieces s ON r.recordId = s.rollrecID)
left join TensileProducts tp on r.FinalProd = tp.sqlproduct
WHERE r.RollShiftID like '07/17/1525%A' and s.RepairCode like '%F%'
order by s.repaircode

我得到“无法对包含聚合或子查询的表达式执行聚合函数”。错误。我如何总结子选择? 顺便说一下,这个查询返回(没有总和)......

F   3.22
F   3.22
F   2.23
F   3.96

我正在寻找F 12.63

谢谢,

1 个答案:

答案 0 :(得分:1)

我认为您只需要将总和设置为子查询并将其连接到其他查询。我认为这样可行,但我没有表格可以测试。

SELECT s.RepairCode
,sum(tot.totals) as tons
FROM NYS2Reheat r 
INNER JOIN NYS2SawPieces s ON r.recordId = s.rollrecID
left join TensileProducts tp on r.FinalProd = tp.sqlproduct
Left JOIN (SELECT s.REpairCode, isnull (Sum(Convert(numeric(8,2), ((tp2.FootWeight/12)*(FinishLength)/2000))),0) 'totals'
FROM NYS2Reheat r2 
INNER JOIN NYS2SawPieces s2 ON r2.recordId = s2.rollrecID
left join TensileProducts tp2 on r2.FinalProd = tp2.sqlproduct
where s2.recordid = s.recordid and tp2.Active = 1
Group by s.RepairCode) tot
on tot.repaidcode = s.RepairCode

WHERE r.RollShiftID like '07/17/1525%A' and s.RepairCode like '%F%'
group by s.RepairCode
order by s.repaircode