我如何编写一个选择语句来评估多个字段以填充新的字段值?

问题描述

我翻遍了一堆帖子,但没有找到适合我的问题的帖子。我试过 CASE 和 IIF,但似乎都不起作用。我妻子会说我不知道​​如何正确使用 Google。我有一个导出加载到 sql Server Express 15 和 Access 16 (365) 中。我可以使用任何一个来实现我的目标。我有一个从数据导出导入的表,其中包含以下字段:

logonID
DeptName
CertDate
CertStatus
PastDue

我需要编写一个查询,该查询将提供具有适当值的状态字段,其中:

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’)

其他都是空白

解决方法

在 SQL Server 中,您将使用 CASE 表达式。

我使用了您示例中的字段名称,但您没有提供表名,因此您必须更新示例以满足您的需要。

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 中的样子。

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
,

您尝试了哪些语法? 我会去类似的东西

<div class="nameintro">
  <div class="box">
    <div class="one block1"></div>
    <h1 class="two">XYZ<span>.</span></h1>
  </div>
  <div class="role">
    <div class="block2"></div>
    <p>AMATEUR ARTIST</p>
  </div>
</div>