简体   繁体   English

SQL 案例语句或子查询

[英]SQL Case Statement or Sub-Query

In a query we have in an application, we have a situation where we are bringing records back that are open, closed, and archived with a date associated with them.在我们在应用程序中的查询中,我们会带回打开、关闭和存档的记录,并带有与之关联的日期。 This is a table associated and joined with a main table.这是一个与主表关联并连接的表。 The table could have 1 to 3 records associated with the same ID of the main table depending if the record has been opened, closed, and/or archived.该表可能有 1 到 3 条记录与主表的相同 ID 相关联,具体取决于记录是否已打开、关闭和/或存档。 The three stages essentially of open, closed, and archived.三个阶段基本上是打开、关闭和归档。

What we're looking to do is this: When EStatusID = 1 (Which means open) we need the DateClosed to read as blank (because it's not closed or archived yet)我们要做的是:当 EStatusID = 1(这意味着打开)时,我们需要 DateClosed 读取为空白(因为它尚未关闭或存档)

     SELECT
     E.EID,
     EStatus.EStatusID,
     FORMAT (EStatus.DateCreated, 'MM/dd/yyyy') as DateClosed,

I won't bore you with the rest of the query because it's long and not useful to the question.我不会对查询的 rest 感到厌烦,因为它很长而且对这个问题没有用处。 So we need some kind of Case statement or sub query or something in the Select to accomplish this task.所以我们需要某种Case语句或子查询或Select中的东西来完成这个任务。

You can use a case expression:您可以使用case表达式:

CASE WHEN EStatus.EStatusID <> 1 THEN FORMAT (EStatus.DateCreated, 'MM/dd/yyyy') END
    AS DateClosed,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM