编写此T-SQL查询的更有效方法?从EXISTS语句传递列?

时间:2019-01-09 18:38:22

标签: sql-server

有没有更好的方法来写这个?首先,我指的是THEN语句,基本上重复了CASE WHEN EXISTS语句。最好在SELECT DateJoined语句内使用EXISTS,然后在THEN的该语句外使用它。有可能吗?

SELECT
    Email,
    CASE 
       WHEN EXISTS (SELECT 1
                    FROM _Subscribers
                    WHERE EmailAddress = Email)
          THEN (SELECT DateJoined 
                FROM _Subscribers 
                WHERE EmailAddress = Email)
          ELSE LastModifiedDate
    END as CreatedDate
FROM 
    [Optin Monster]

1 个答案:

答案 0 :(得分:0)

感谢@Lamak在此方面的帮助。这成功了。

SELECT
  om. Email,
  CASE WHEN s.DateJoined <> '' OR s.DateJoined IS NOT NULL
  THEN s.DateJoined
  ELSE LastModifiedDate
  END as CreatedDate
FROM [Optin Monster] om
LEFT JOIN _Subscribers s
ON om.Email = s.EmailAddress