我正在使用sql server 2005。 我有以下字段名为referenceInfo。如果长度大于11,则为身份编号。如果长度小于12则是批准。
问题是它会跳到第二行。如何在同一行中制作它?
http://postimg.org/image/8h4njmfsp/
CASE
WHEN LEN(CONVERT(varchar(12), GUEST_CHECK_LINE_ITEM_HIST.referenceInfo)) > 11 THEN (+'"') + RTRIM(CONVERT(varchar(12), GUEST_CHECK_LINE_ITEM_HIST.referenceInfo)) + '"'
ELSE '""'
END AS [Identity Number],
CASE
WHEN LEN(CONVERT(varchar(12), GUEST_CHECK_LINE_ITEM_HIST.referenceInfo)) < 12 THEN (+'"') + RTRIM(CONVERT(varchar(12), GUEST_CHECK_LINE_ITEM_HIST.referenceInfo)) + '"'
ELSE '""'
END AS [ApprovalCode]
由于
答案 0 :(得分:1)
我不确定,但似乎您正在尝试将两行合并为一行。既然你没有提供完整的sql,请尝试这样的事情(猜测ReceiptNo
是唯一的)。这两个字段都需要t1
或t2
:
select all_other_fields
, max(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 '""'
end) as [identity number]
, max(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 '""'
end) as [approvalcode]
from guest_check_line_item_hist t1
join guest_check_line_item_hist t2
on t1.ReceiptNo = t2.ReceiptNo
group
by all_other_fields