TSQL Max功能无法正常工作

时间:2013-04-22 17:25:29

标签: join max

使用TSQL - 试图获取最新的MAX(HAR_ASSESSMENT.SESSION_DATE)但我的查询返回最早的评估日期而不是最近的评估日期。我认为它与SERVICE_DELIVERIES连接和参数有关...任何建议都将受到赞赏。

**SELECT
  HAR_ASSESSMENTS.NEXT_SESSION_DATE
  ,HAR_CONSUMERS.CLIENT_ID
  ,HAR_CONSUMERS.FULL_NAME
  ,MAX (HAR_SERVICE_DELIVERIES.SERVICE_PERIOD_START) AS Max_SERVICE_PERIOD_START
  ,HAR_SERVICE_DELIVERIES.SERVICE
  ,HAR_ASSESSMENTS.ASSESSFORM_NAME
  ,MAX(HAR_ASSESSMENTS.SESSION_DATE) AS Max_SESSION_DATE
  ,HAR_SERVICE_DELIVERIES.AGENCY
  ,HAR_SERVICE_DELIVERIES.PROVIDER
  ,HAR_SERVICE_DELIVERIES.FUND_IDENTIFIER
FROM
  HAR_CONSUMERS
  INNER JOIN HAR_ASSESSMENTS
    ON HAR_CONSUMERS.CONSUMER_UUID = HAR_ASSESSMENTS.CONSUMER_UUID
  INNER JOIN HAR_SERVICE_DELIVERIES
    ON HAR_CONSUMERS.CONSUMER_UUID = HAR_SERVICE_DELIVERIES.CONSUMER_UUID
WHERE
  HAR_SERVICE_DELIVERIES.SERVICE_PERIOD_START >= @SERVICE_PERIOD_START
  AND HAR_SERVICE_DELIVERIES.SERVICE_PERIOD_START <= @SERVICE_PERIOD_START2
  AND HAR_SERVICE_DELIVERIES.SERVICE IN (@SERVICE)
  AND HAR_ASSESSMENTS.ASSESSFORM_NAME IN (@ASSESSFORM_NAME)
  AND HAR_ASSESSMENTS.AGENCY IN (@AGENCY)
GROUP BY
HAR_ASSESSMENTS.NEXT_SESSION_DATE
  ,HAR_CONSUMERS.CLIENT_ID
  ,HAR_CONSUMERS.FULL_NAME
  ,HAR_SERVICE_DELIVERIES.SERVICE
  ,HAR_ASSESSMENTS.ASSESSFORM_NAME
  ,HAR_SERVICE_DELIVERIES.AGENCY
  ,HAR_SERVICE_DELIVERIES.PROVIDER
  ,HAR_SERVICE_DELIVERIES.FUND_IDENTIFIER**

0 个答案:

没有答案