开发者

T-SQL Getting duplicate rows returned

The following code section is returning multiple columns for a few records.

SELECT a.ClientID,ltrim(rtrim(c.FirstName)) + ' ' +

case when c.MiddleName <> '' then
ltrim(rtrim(c.MiddleName)) + '. ' 
else '' 
end +

ltrim(rtrim(c.LastName)) as ClientName, a.MISCode, b.Address, b.City, dbo.ClientGetEnrolledPrograms(CONVERT(int,a.ClientID)) as Abbreviation
FROM ClientDetail a
JOIN Address b on(a.PersonID = b.PersonID)
JOIN Person c on(a.PersonID = c.PersonID)
LEFT JOIN ProgramEnrollments d on(d.ClientID = a.ClientID and d.Status = 'Enrolled' and d.HistoricalPKID is null)
LEFT JOIN Program e on(d.ProgramID = e.ProgramID and e.HistoricalPKID is null)
WHERE a.MichiganWorksData=1

I've isolate开发者_如何学编程d the issue to the ProgramEnrollments table. This table holds one-to-many relationships where each ClientID can be enrolled in many programs. So for each program a client is enrolled in, there is a record in the table.

The final result set is therefore returning a row for each row in the ProgramEnrollments table based on these joins.

I presume my join is the issue but I don't see the problem.

Thoughts/Suggestions?

Thanks,

Chuck


The JOIN is not the issue, it's doing what it's meant to do with a one-to-many relationship

You could use a GROUP BY statement on your query, or alternatively use a sub-select to return DISTINCT values from the ProgramEnrollments/Program tables.

You don't seem to be using data from the ProgramEnrollments or Program tables so are they needed in the query (I presume they are, just thought I'd ask the question).


You don't actually appear to be using any columns in ProgramEnrollments or Program, so try removing those 2 JOINs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜