我有一张付款表。我想根据收据编号选择几列。然后,我想根据一些值显示条件状态列。
我写了一条SQL语句,可以很好地做到这一点:
代码
SELECT
[Number], [Name], [Amount],
CASE
WHEN [Downloaded by] IS NULL
AND [Request Accepted?] IS NULL
THEN 'Sent to Payments'
WHEN [Downloaded by] IS NOT NULL
AND [Request Accepted?] IS NULL
THEN 'Received by Payments'
WHEN [Downloaded by] IS NOT NULL
AND [Request Accepted?] IS NOT NULL
THEN 'Sent to Finance'
END AS 'Real-time Status'
FROM
[DB]
WHERE
[Request Receipt Id] = 'PR0001'
我正在构建一个表单,用户可以在其中滚动浏览这些值,并且希望显示数据库中的另一列。但是,我不想在主搜索框中显示此列。因此,我将搜索框设置为仅包含4x列(“数字”,“名称”,“金额”和“实时状态”)。
这个想法是,如果我有第5列,除非他们单击它,否则它将不会显示在主用户窗体中,并且它将出现在另一个框中。
我的问题是设置此SQL语句以根据需要组织数据:
[Number], [Name], [Amount], [Real-time Status], [Description]
我用来执行此操作的代码是:
SELECT
[Number], [Name], [Amount],
CASE
WHEN [Downloaded by] IS NULL
AND [Request Accepted?] IS NULL
THEN 'Sent to Payments'
WHEN [Downloaded by] IS NOT NULL
AND [Request Accepted?] IS NULL
THEN 'Received by Payments'
WHEN [Downloaded by] IS NOT NULL
AND [Request Accepted?] IS NOT NULL
THEN 'Sent to Finance'
END AS 'Real-time Status'
FROM
TEP_Payments_Table
WHERE
[Request Receipt Id] = 'PR0001'
UNION
SELECT
[Description]
WHERE
[Request Receipt Id] = 'PR0001'
但是,当我尝试这样做时,SQL Server不喜欢它并引发错误。
你们能告诉我我要去哪里错了,什么是实现我想要的最有效方法。
谢谢
答案 0 :(得分:2)
如果所有数据都来自一个表,则只需在查询中添加描述字段。
SELECT [Number],[Name],[Amount],
CASE WHEN [Downloaded by] is null and [Request Accepted?] is null then 'Sent to Payments'
WHEN [Downloaded by] is not null and [Request Accepted?] is null then 'Received by Payments'
When [Downloaded by] is not null and [Request Accepted?] is not null then 'Sent to Finance'
END as 'Real-time Status',
Description
from TEP_Payments_Table where [Request Receipt Id] = 'PR0001'