我有一张桌子,我正在存储津贴和扣减。如果BaseCode为1,那么它是一个余量,如果BaseCode是2,那么它是扣除。 我试图将列转换为2列。
津贴扣除 什么东西
SELECT (
SELECT INN.FullName AS Benefit
FROM [AppCNF].[tbl_AllowanceOrBenefitType] INN
WHERE INN.BASECODE = 1
AND INN.ID = OUTR.ID
)
, FullName AS Deductions
FROM [AppCNF].[tbl_AllowanceOrBenefitType] OUTR
WHERE BASECODE = 2
这是我到目前为止所尝试的内容,但它给予Allowances NULL。
答案 0 :(得分:5)
不确定您到底在做什么,但很可能您可以使用CASE
:
SELECT
ID
,CASE WHEN BASECODE = 1 THEN FullName END AS Benefit
,CASE WHEN BASECODE = 2 THEN FullName END AS Deductions
-- ,... other columns ...
FROM [AppCNF].[tbl_AllowanceOrBenefitType]
;
答案 1 :(得分:1)
您可以使用CASE
来调整列中的结果。
SELECT
IsAllowance = CASE WHEN OUTR.BASECODE = 1 THEN 'Yes' END,
IsDeduction = CASE WHEN OUTR.BASECODE = 2 THEN 'Yes' END,
OUTR.*
FROM
[AppCNF].[tbl_AllowanceOrBenefitType] OUTR