首先我是一名程序员,我没有开设任何SQL课程,并且自学成才。我想要的所有数据都很好,但是我在日期字段上设置了MAX,并且仍然为我提供了表格中的所有日期。我只想要最新的。我也有多个联接,所以我确定设置不正确。任何帮助将不胜感激。
我正在使用Pentaho作为作家
SELECT DISTINCT
"RPT_CLIENT_PAYER_PLAN"."PAYER_PLAN_NAME",
"RPT_CLIENT"."CLIENT_NUMBER",
"RPT_CLIENT_PROGRAMS"."PROGRAM_ID",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P1_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P1_DESCRIPTION",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P2_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P2_DESCRIPTION",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P3_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P3_DESCRIPTION",
"RPT_CLIENT_PROGRAMS"."PROGRAM",
"RPT_CLIENT_PROGRAMS"."BEGIN_DATE",
"RPT_CLIENT"."AGE",
"RPT_CLIENT"."GENDER",
"RPT_CLIENT"."MILITARY_SERVICES",
"RPT_CLIENT"."MILITARY_STATUS",
"RPT_SVCDOC_TOBACCO"."SMOKING_STATUS",
"RPT_SVCDOC_REFERRAL"."REFER_COMMENT",
"RPT_SVCDOC_REFERRAL"."REASON_FOR_REFERRAL",
"RPT_CLIENT_PAYER_PLAN"."END_DATE",
"RPT_CLIENT_PROGRAMS"."END_DATE",
MAX("RPT_CLIENT_DSM5_DIAG"."EFFECTIVE_DATE") as max_date
FROM
"RPT_CLIENT_PROGRAMS" INNER JOIN "RPT_CLIENT" ON "RPT_CLIENT_PROGRAMS"."CLIENT_ID" = "RPT_CLIENT"."CLIENT_ID"
INNER JOIN "RPT_CLIENT_DSM5_DIAG" ON "RPT_CLIENT_PROGRAMS"."CLIENT_ID" = "RPT_CLIENT_DSM5_DIAG"."CLIENT_ID"
INNER JOIN "RPT_CLIENT_PAYER_PLAN" ON "RPT_CLIENT_DSM5_DIAG"."CLIENT_ID" = "RPT_CLIENT_PAYER_PLAN"."CLIENT_ID"
INNER JOIN "RPT_SVCDOC_TOBACCO" ON "RPT_CLIENT_PAYER_PLAN"."CLIENT_ID" = "RPT_SVCDOC_TOBACCO"."CLIENT_ID"
INNER JOIN "RPT_SVCDOC_REFERRAL" ON "RPT_CLIENT"."CLIENT_NUMBER" = "RPT_SVCDOC_REFERRAL"."CLIENT_NUMBER"
WHERE
"RPT_CLIENT_PROGRAMS"."PROGRAM_ID" IN (1087)
and "RPT_CLIENT_PROGRAMS"."BEGIN_DATE" >= ${Begin_Date}
and "RPT_CLIENT_PAYER_PLAN"."END_DATE" IS NULL
GROUP BY
"RPT_CLIENT_PAYER_PLAN"."PAYER_PLAN_NAME",
"RPT_CLIENT"."CLIENT_NUMBER",
"RPT_CLIENT_PROGRAMS"."PROGRAM_ID",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P1_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P1_DESCRIPTION",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P2_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P2_DESCRIPTION",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P3_CODE",
"RPT_CLIENT_DSM5_DIAG"."DSM5_P3_DESCRIPTION",
"RPT_CLIENT_PROGRAMS"."PROGRAM",
"RPT_CLIENT_PROGRAMS"."BEGIN_DATE",
"RPT_CLIENT"."AGE",
"RPT_CLIENT"."GENDER",
"RPT_CLIENT"."MILITARY_SERVICES",
"RPT_CLIENT"."MILITARY_STATUS",
"RPT_SVCDOC_TOBACCO"."SMOKING_STATUS",
"RPT_SVCDOC_REFERRAL"."REFER_COMMENT",
"RPT_SVCDOC_REFERRAL"."REASON_FOR_REFERRAL",
"RPT_CLIENT_PAYER_PLAN"."END_DATE",
"RPT_CLIENT_PROGRAMS"."END_DATE",
"RPT_CLIENT_DSM5_DIAG"."EFFECTIVE_DATE"
希望只获得最新的DMS-5生效日期,但仍然给我一切。
答案 0 :(得分:1)
您在max
列上有EFFECTIVE_DATE
,但它也是group by
的一部分。由于它是分组依据的一部分,因此该列的每个唯一值都将单独显示。从group by
删除它,它应该可以工作。