SQL 从两个日期时间列中获取最大日期和时间

SQL to get the max date and time from two DateTime Columns

我在 Yellowfin 中遇到了一个 SQL 查询(所以我不确定 SQL 版本)。

我想从 table 获取最大修订版,但是 table 包含一个日期字段和一个时间字段(均为 DateTime 格式)。

到目前为止我写的是:

SELECT 
      a.MemberNo, 
      HealthScale, 
      status,
      a.EffDateTime = DATEADD(day, 0, DATEDIFF(day, 0, EffDate)) +
        DATEADD(day, 0 - DATEDIFF(day, 0, EffDate), EffTime)
FROM MembershipPlans AS a 
INNER JOIN
(
SELECT 
      MemberNo, 
      MAX(EffDateTime = DATEADD(day, 0, DATEDIFF(day, 0, EffDate)) +
        DATEADD(day, 0 - DATEDIFF(day, 0, EffDate), EffTime) 
FROM MembershipPlans GROUP BY MemberNo
) AS b
ON a.MemberNo = b.MemberNo AND a.EffDateTime = b.EffDateTime

我收到一条错误消息:

SQL statement is invalid. The error message returned from the database was: Incorrect syntax near '='.

我想要的结果是为每个成员编号获取一行,显示最大有效日期和匹配的健康等级和状态。 如有任何帮助,我们将不胜感激

您的查询应该是这样的:

  SELECT a.MemberNo,HealthScale,status,a.EffDateTime, b.EffDateTime FROM
  (SELECT 
        MemberNo, 
        HealthScale, 
        status,
        DATEADD(day, 0, DATEDIFF(day, 0, EffDate)) +
        DATEADD(day, 0 - DATEDIFF(day, 0, EffDate), EffTime) AS EffDateTime
  FROM MembershipPlans) AS a 
  INNER JOIN
  (SELECT 
        MemberNo, 
        MAX(DATEADD(day, 0, DATEDIFF(day, 0, EffDate)) +
        DATEADD(day, 0 - DATEDIFF(day, 0, EffDate), EffTime)) AS EffDateTime 
   FROM MembershipPlans GROUP BY MemberNo) AS b
   ON a.MemberNo = b.MemberNo