gpt4 book ai didi

sql - SQL 中 Case 列的乘法

转载 作者:行者123 更新时间:2023-12-02 09:16:53 24 4
gpt4 key购买 nike

我有一个查询,其中基于案例有两列,我希望乘以 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但它不起作用。

请指教

请注意,我还需要外连接中的其他表

最佳答案

您可以尝试将 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

关于sql - SQL 中 Case 列的乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46234475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com