You need to use a GROUP BY anytime you use an aggregate function. The HAVING clause allows you to put a filter condition on the aggregate.
The simplest thing to try is to reproduce the entire select clause in the group by with the exception of the aggregate function.
Also... you won't need the distinct in the select clause because the group by will cause each row in the output to be distinct anyway.
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
The simplest thing to try is to reproduce the entire select clause in the group by with the exception of the aggregate function.
Code:
SELECT Distinct A.MBR_SSN_NBR,
CASE
when A.MBR_F_EMP_DT between '2010-07-01' and '2011-06-30' then 'NEW'
when A.MBR_L_EMP_DT between '2010-07-01' and '2011-06-30' then 'TERMINATED'
when C.mbr_stat_cd = '3' then 'DECEASED'
END AS STATUS,
min(C.MBR_F_ENROL_DT) as "START DATE"
FROM DSNP.PR01_T_MBR_EMPR A,
DSNP.PR01_T_MBR_HIST B,
DSNP.PR01_T_MBR_SYS C
WHERE A.MBR_SSN_NBR=B.MBR_SSN_NBR
AND A.MBR_SSN_NBR=C.MBR_SSN_NBR
AND B.MBR_HIST_SVC_CR_DT BETWEEN '2010-07-01' AND '2011-06-30'
Group By A.MBR_SSN_NBR,
CASE
when A.MBR_F_EMP_DT between '2010-07-01' and '2011-06-30' then 'NEW'
when A.MBR_L_EMP_DT between '2010-07-01' and '2011-06-30' then 'TERMINATED'
when C.mbr_stat_cd = '3' then 'DECEASED'
END
Also... you won't need the distinct in the select clause because the group by will cause each row in the output to be distinct anyway.
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom