我有一个查询,其中我有两个基于Case的列,我希望将Qty_Sold * UnitPrice乘以得到结果
SELECT CASE
WHEN RPT_ITM_D.F1034 = 3
THEN F64
ELSE 0
END AS Qty_Sold
,CASE
WHEN [POS_TAB].F81 = 1
THEN Cast(CONVERT(DECIMAL(10, 3), F65 / NULLIF(F64, 0)) AS NVARCHAR) * cast(1.15 AS NUMERIC(10, 3))
ELSE Cast(CONVERT(DECIMAL(10, 3), F65 / NULLIF(F64, 0)) AS NVARCHAR)
END AS UnitPrice
FROM [dbo].[RPT_ITM_D]
LEFT OUTER JOIN [dbo].[POS_TAB] ON (RPT_ITM_D.F01 = POS_TAB.F01)
LEFT OUTER JOIN [dbo].SDP_TAB ON (POS_TAB.F04 = SDP_TAB.F04)
LEFT OUTER JOIN [dbo].DEPT_TAB ON (SDP_TAB.F03 = DEPT_TAB.F03)
WHERE RPT_ITM_D.F1034 IN (
3
,3012
)
AND RPT_ITM_D.F254 = convert(VARCHAR, getdate(), 101)
我尝试了This Example,但它无效。
请咨询
请注意,我也需要在外部加入中使用其他表格
答案 0 :(得分:3)
您可以尝试将CTE用作:
With CTE as
(SELECT CASE
WHEN RPT_ITM_D.F1034 = 3
THEN F64
ELSE 0
END AS Qty_Sold
,CASE
WHEN [POS_TAB].F81 = 1
THEN Cast(CONVERT(DECIMAL(10, 3), F65 / NULLIF(F64, 0)) AS NVARCHAR) * cast(1.15 AS NUMERIC(10, 3))
ELSE Cast(CONVERT(DECIMAL(10, 3), F65 / NULLIF(F64, 0)) AS NVARCHAR)
END AS UnitPrice
FROM [dbo].[RPT_ITM_D]
LEFT OUTER JOIN [dbo].[POS_TAB] ON (RPT_ITM_D.F01 = POS_TAB.F01)
LEFT OUTER JOIN [dbo].SDP_TAB ON (POS_TAB.F04 = SDP_TAB.F04)
LEFT OUTER JOIN [dbo].DEPT_TAB ON (SDP_TAB.F03 = DEPT_TAB.F03)
WHERE RPT_ITM_D.F1034 IN (3,3012)
AND RPT_ITM_D.F254 = convert(VARCHAR, getdate(), 101)
)
SELECT
(Qty_Sold * UnitPrice) AS result
FROM CTE
答案 1 :(得分:2)
以这种方式将查询包装到子查询中:
SELECT t.*,
Qty_Sold * UnitPrice As my_result
FROM (
SELECT CASE .....
..... your query goes here
) t