如何编写 select 语句来评估多个字段以填充新字段值?
how do I write a select statement that will evaluate multiple fields to populate a new field value?
我翻了一堆帖子,但没有找到适合我问题的帖子。我试过 CASE 和 IIF,但 none 似乎有效。我妻子会说我不知道如何 Google 正确。我将导出加载到 SQL Server Express 15 和 Access 16 (365) 中。我可以使用任何一个来实现我的目标。我从数据导出中导入了一个 table,其中包含以下字段:
LogonID
DeptName
CertDate
CertStatus
PastDue
我需要编写一个查询,为 Status 字段提供适当的值,其中:
If ((CertDate Is Not Null) AND (CertStatus = ‘Current’) AND (PastDue = ‘No’)) then
Status = ‘ReadOnTime’)
If ((CertDate Is Not Null) AND (CertStatus = ‘Current’) AND (PastDue = ‘Yes’)) then
Status = ‘ReadLate’)
If ((CertDate Is Null) AND (CertStatus = ‘Pending’) AND (PastDue = ‘Yes’)) then
Status = ‘UnReadLate’)
其他都是空白
您尝试了哪些语法?
我会选择
update table my_table set
Status = (case when condition1 then 'ReadOnTime'
else when condition2 then 'ReadLate'
else when condition2 then 'UnReadLate'
else '' end)
在 SQL 服务器中,您将使用 CASE 表达式。
我使用了您示例中的字段名称,而您没有提供 table 名称,因此您必须更新示例以满足您的需要。
SELECT CASE WHEN [CertDate] IS NOT NULL
AND [CertStatus] = 'Current'
AND [PassDue] = 'No' THEN 'ReadOnTime'
WHEN [CertDate] IS NOT NULL
AND [CertStatus] = 'Current'
AND [PassDue] = 'Yes' THEN 'ReadLate'
WHEN [CertDate] IS NULL
AND [CertStatus] = 'Pending'
AND [PassDue] = 'Yes' THEN 'UnReadLate'
ELSE ''
END AS [Status]
FROM [YourTableName];
这是查询在 Access SQL 中的样子 SQL。
Select
LogonID,
DeptName,
CertDate,
CertStatus,
PastDue,
IIf (CertDate Is Not Null AND CertStatus = "Current" AND PastDue = "No", "ReadOnTime",
IIf (CertDate Is Not Null AND CertStatus = "Current" AND PastDue = "Yes", "ReadLate",
IIf (CertDate Is Null AND CertStatus = "Pending" AND PastDue = "Yes", "UnReadLate",
""))) As Status
From Table
我翻了一堆帖子,但没有找到适合我问题的帖子。我试过 CASE 和 IIF,但 none 似乎有效。我妻子会说我不知道如何 Google 正确。我将导出加载到 SQL Server Express 15 和 Access 16 (365) 中。我可以使用任何一个来实现我的目标。我从数据导出中导入了一个 table,其中包含以下字段:
LogonID
DeptName
CertDate
CertStatus
PastDue
我需要编写一个查询,为 Status 字段提供适当的值,其中:
If ((CertDate Is Not Null) AND (CertStatus = ‘Current’) AND (PastDue = ‘No’)) then
Status = ‘ReadOnTime’)
If ((CertDate Is Not Null) AND (CertStatus = ‘Current’) AND (PastDue = ‘Yes’)) then
Status = ‘ReadLate’)
If ((CertDate Is Null) AND (CertStatus = ‘Pending’) AND (PastDue = ‘Yes’)) then
Status = ‘UnReadLate’)
其他都是空白
您尝试了哪些语法? 我会选择
update table my_table set
Status = (case when condition1 then 'ReadOnTime'
else when condition2 then 'ReadLate'
else when condition2 then 'UnReadLate'
else '' end)
在 SQL 服务器中,您将使用 CASE 表达式。
我使用了您示例中的字段名称,而您没有提供 table 名称,因此您必须更新示例以满足您的需要。
SELECT CASE WHEN [CertDate] IS NOT NULL
AND [CertStatus] = 'Current'
AND [PassDue] = 'No' THEN 'ReadOnTime'
WHEN [CertDate] IS NOT NULL
AND [CertStatus] = 'Current'
AND [PassDue] = 'Yes' THEN 'ReadLate'
WHEN [CertDate] IS NULL
AND [CertStatus] = 'Pending'
AND [PassDue] = 'Yes' THEN 'UnReadLate'
ELSE ''
END AS [Status]
FROM [YourTableName];
这是查询在 Access SQL 中的样子 SQL。
Select
LogonID,
DeptName,
CertDate,
CertStatus,
PastDue,
IIf (CertDate Is Not Null AND CertStatus = "Current" AND PastDue = "No", "ReadOnTime",
IIf (CertDate Is Not Null AND CertStatus = "Current" AND PastDue = "Yes", "ReadLate",
IIf (CertDate Is Null AND CertStatus = "Pending" AND PastDue = "Yes", "UnReadLate",
""))) As Status
From Table