我有以下SQL,包括CASE
的{{1}}语句。我想修改D.TIN_TYPE
语句(或使用替代方法)以添加到第二WHEN条件,以便CASE
在WHEN D.TIN_TYPE = 'S' AND
字符串中有一个逗号(,),然后中断名称使用LEFT / CHARINDEX和RIGHT / CHARINDEX函数分成两列,否则按原样显示字符串(如果字符串中不包含逗号)。该列的格式不统一为 Lastname,Firstname (有些是 Firstname Lastname ),因此这就是为什么我需要检查字符串中是否存在逗号的原因。我该怎么做?
D.PAYEE_NAME1
编辑:
这是经过编辑的SQL,在CASE语句中使用额外的WHEN。它运行,但是对于姓氏列(RIGHT函数),它同时返回名字和姓氏。我如何才能返回姓氏(逗号前的字符)?我也尝试为每个WHEN使用END AS'First name'和END AS'Last Name',但是出现语法错误。有没有一种方法可以为列指定不同的名称?
SELECT D.PAYER_TIN, '001', YEAR( (CONVERT(CHAR(10),A.PYMNT_DT,121))), D.PAYEE_TIN,
CASE WHEN D.TIN_TYPE = 'F' THEN '001'
WHEN D.TIN_TYPE = 'S' THEN '002'
END AS 'Taxpayer Identification Type',
A.VENDOR_ID,
CASE WHEN D.TIN_TYPE = 'F' THEN LEFT(D.PAYEE_NAME1, 30)
WHEN D.TIN_TYPE = 'S' AND --Additional logic for existence of comma--
ELSE D.PAYEE_NAME1
END AS 'PAYEE_NAME1',
D.PAYEE_NAME1,
D.PAYEE_NAME2,
D.PAYEE_ADDRESS1,
LEFT(PAYEE_CITYSTZIP,CHARINDEX(',', PAYEE_CITYSTZIP)-1),
SUBSTRING(PAYEE_CITYSTZIP, CHARINDEX(',', PAYEE_CITYSTZIP)+2, 2),
RIGHT(PAYEE_CITYSTZIP, LEN(PAYEE_CITYSTZIP) - CHARINDEX(',', PAYEE_CITYSTZIP)-7),
'US',
D.WTHD_BOX1,
D.WTHD_BOX2,
D.WTHD_BOX3,
FROM PS_WTHD_TRXN_TBL A, PS_TMP_1099_COPYB D, PS_GHS_1099_PA_VW2 E
WHERE ( A.WTHD_TYPE = D.WTHD_TYPE
AND A.VENDOR_ID = D.VENDOR_ID
AND YEAR( A.PYMNT_DT) = '2018')
GROUP BY D.PAYER_TIN, YEAR( (CONVERT(CHAR(10),A.PYMNT_DT,121))), D.PAYEE_TIN, CASE WHEN D.TIN_TYPE = 'F' THEN '001'
WHEN D.TIN_TYPE = 'S' THEN '002'
END, A.VENDOR_ID, D.PAYEE_NAME1, D.PAYEE_NAME2, D.PAYEE_ADDRESS1,
LEFT(PAYEE_CITYSTZIP,CHARINDEX(',', PAYEE_CITYSTZIP)-1),
SUBSTRING(PAYEE_CITYSTZIP, CHARINDEX(',', PAYEE_CITYSTZIP)+2, 2),
RIGHT(PAYEE_CITYSTZIP, LEN(PAYEE_CITYSTZIP) - CHARINDEX(',', PAYEE_CITYSTZIP)-7),
D.WTHD_BOX1, D.WTHD_BOX2, D.WTHD_BOX3, D.WTHD_BOX6, D.WTHD_BOX7, D.WTHD_BOX14, D.WTHD_BOX16, D.WTHD_BOX18
, CASE WHEN D.TIN_TYPE = 'F' THEN LEFT(D.PAYEE_NAME1, 30)
ELSE D.PAYEE_NAME1
END
答案 0 :(得分:1)
您将需要为此创建多个case语句。我的建议是创建3个case语句:一个用于非拆分,一个用于第一个拆分,一个用于第二个拆分。
要检查逗号,可以使用类似以下的内容:
select
split1=case
WHEN CHARINDEX(',','a,b') > 0 AND D.TIN_TYPE = 'S' THEN left('a,b', CHARINDEX(',','a,b')-1)
END,
split2=case
WHEN CHARINDEX(',','a,b') > 0 AND D.TIN_TYPE = 'S' THEN right('a,b', len('a,b')-CHARINDEX(',','a,b'))
END
Results:
split1 split2
a b
请参见CASE (Transact-SQL) Example B
编辑: 尝试从您编辑过的查询中提取此较短的查询。使它起作用,然后开始在GROUP BY中添加其余条件,我想这就是您的问题所在。
看看我对PAYEE_FULLNAME,PAYEE_FIRSTNAME,PAYEE_LASTNAME做了什么。
SELECT D.PAYER_TIN, '001', 13679089, YEAR( (CONVERT(CHAR(10),A.PYMNT_DT,121))), D.PAYEE_TIN,
CASE WHEN D.TIN_TYPE = 'F' THEN '001'
WHEN D.TIN_TYPE = 'S' THEN '002'
END AS 'Taxpayer Identification Type',
A.VENDOR_ID,
PAYEE_FULLNAME=CASE
WHEN D.TIN_TYPE = 'F' THEN LEFT(D.PAYEE_NAME1, 30)
ELSE D.PAYEE_NAME1
END ,
PAYEE_FIRSTNAME=CASE
WHEN D.TIN_TYPE = 'S' AND CHARINDEX(',',D.PAYEE_NAME1) > 0 THEN LEFT(D.PAYEE_NAME1,CHARINDEX(',', D.PAYEE_NAME1)-1) --END AS 'First Name'
END ,
PAYEE_LASTNAME=CASE
WHEN D.TIN_TYPE = 'S' AND CHARINDEX(',',D.PAYEE_NAME1) > 0 THEN RIGHT(D.PAYEE_NAME1, LEN(PAYEE_CITYSTZIP) - CHARINDEX(',', D.PAYEE_NAME1)+1 ) --END AS 'Last Name'
END
FROM PS_WTHD_TRXN_TBL A, PS_TMP_1099_COPYB D, PS_GHS_1099_PA_VW2 E
WHERE ( A.WTHD_TYPE = D.WTHD_TYPE
AND A.VENDOR_ID = D.VENDOR_ID
AND YEAR( A.PYMNT_DT) = '2018'
AND A.WTHD_CLASS IN ('01','02','07')
AND A.VENDOR_ID = E.VENDOR_ID
AND A.VNDR_LOC = E.VNDR_LOC
AND E.YEAR = '2018'
AND D.WTHD_CNTL_ID = 'TGC'
AND D.WTHD_BOX18 > 0.00
AND A.VENDOR_ID <> '80577A' )