我正在使用SQL Server 2005.我试图合并两行。
需要帮助才能排除身份编号和ApprovalCode NULL值。
SELECT DISTINCT CASE
WHEN (CONVERT(varchar(12), t1.referenceInfo)) like '1000%' THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t1.referenceInfo)) + '"'
ELSE
'0'
END AS [ identity number ],
CASE
WHEN LEN(ISNUMERIC(CONVERT(varchar(12), t2.referenceInfo))) < 12 THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t2.referenceInfo)) + '"'
WHEN (CONVERT(varchar(12), t2.referenceInfo)) LIKE '10%' THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t2.referenceInfo)) + '"'
ELSE
'0'
END AS approvalcode
FROM dbo.GUEST_CHECK_LINE_ITEM_HIST t1
INNER JOIN dbo.GUEST_CHECK_LINE_ITEM_HIST t2
ON t1.guestCheckID = t2.guestCheckID
WHERE t1.guestCheckID = 78362401
AND (t1.referenceInfo) LIKE '1000%'
and (t2.referenceInfo) NOT LIKE '1000%'
--AND LEN(ISNUMERIC(CONVERT(varchar(12), t2.referenceInfo))) < 12
GROUP BY t1.referenceInfo, t2.referenceInfo
参考表如下 http://postimg.org/image/45q41bogf/
我想将身份号码中10000%的值停放,并将批准代码中小于12位的值停放。如果批准代码少于12位,则为null。
感谢能否帮助我解决这个问题
答案 0 :(得分:0)
尝试添加
HAVING [identity number] is not null and approvalcode is not null
最后......
答案 1 :(得分:0)
尝试:
SELECT DISTINCT CASE
WHEN LEN(CONVERT(varchar(12), t1.referenceInfo)) > 11 THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t1.referenceInfo)) + '"'
WHEN LEN(CONVERT(varchar(12), t2.referenceInfo)) > 11 THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t2.referenceInfo)) + '"'
ELSE
'0'
END AS [ identity number ],
CASE
WHEN LEN(CONVERT(varchar(12), t1.referenceInfo)) < 12 THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t1.referenceInfo)) + '"'
WHEN LEN(CONVERT(varchar(12), t2.referenceInfo)) < 12 THEN
(+ '"') + RTRIM(CONVERT(varchar(12), t2.referenceInfo)) + '"'
ELSE
'0'
END AS approvalcode
FROM dbo.GUEST_CHECK_LINE_ITEM_HIST t1
INNER JOIN dbo.GUEST_CHECK_LINE_ITEM_HIST t2
ON t1.guestCheckID = t2.guestCheckID
WHERE t1.guestCheckID = 78362401
AND t2.referenceInfo is not null
and t1.referenceInfo is not null
GROUP BY t1.referenceInfo, t2.referenceInfo
请注意,我更改了where子句的2行。看起来您使用了“&lt;&gt; 0”,您可能打算使用“is not null”。