我试图弄清楚为什么第22行和第25行的第二个BEGIN / END块错误。两种情况都是END。我错过了什么?另外我怎么能做这个查询而不是两个块?
BEGIN
SELECT
l.partNum AS [Part Number]
,l.pricePerUOM AS [Price Per UOM]
,l.uom AS [UOM]
,SUM(CONVERT(DECIMAL(18,12), l.totalCostPerWS/l.pricePerUOM)) AS [Quantity/WS] INTO #temp
FROM tbl_step s
JOIN tbl_line l ON s.stepId = l.stepId
WHERE s.revisionId = @revisionId
AND ISNUMERIC(l.totalCostPerWS) > 0
AND ISNUMERIC(l.pricePerUOM) > 0
GROUP BY l.partNum, l.pricePerUOM, l.uom
END
BEGIN
SELECT
l.partNum AS [Part Number]
,l.pricePerUOM AS [Price Per UOM]
,l.uom AS [UOM]
,CASE
WHEN l.partNum >=2 AND l.uom = 'liter' THEN (SELECT CONVERT(DECIMAL(18,12),[Quantity/WS] * 1000)
END AS [Converted]
,CASE
WHEN l.uom = 'milliliter' THEN (SELECT CONVERT(DECIMAL(18,12),[Quantity/WS] + l.pricePerUOM)
END AS [Qnty/WS + Price per UOM]
FROM #temp
JOIN tbl_line l ON s.stepId = l.stepId
JOIN tbl_step s ON l.stepId = s.stepId
WHERE
ISNUMERIC(l.totalCostPerWS) > 0
AND ISNUMERIC(l.pricePerUOM) > 0
GROUP BY l.partNum, l.pricePerUOM, l.uom
END
感谢您的帮助。 ˚F
答案 0 :(得分:1)
你也有这个:
FROM #temp
JOIN tbl_line l ON s.stepId = l.stepId
JOIN tbl_step s ON l.stepId = s.stepId
我想也许你的意思是:
FROM #temp AS t
JOIN tbl_line l ON t.stepId = l.stepId
JOIN tbl_step s ON l.stepId = s.stepId
但是,这需要#temp有一个stepId列。我认为你需要退后一步,从更简化的查询开始,因为你现在正在做的一大堆没有意义。
答案 1 :(得分:0)
丢失嵌入的SELECT
:
,CASE
WHEN l.partNum >=2 AND l.uom = 'liter' THEN CONVERT(DECIMAL(18,12),[Quantity/WS] * 1000)
END AS [Converted]
,CASE
WHEN l.uom = 'milliliter' THEN CONVERT(DECIMAL(18,12),[Quantity/WS] + l.pricePerUOM)
END AS [Qnty/WS + Price per UOM]