Unix Technical Forum

SQL Server Max Record, multiple table join problems

This is a discussion on SQL Server Max Record, multiple table join problems within the SQL Server forums, part of the Microsoft SQL Server category; --> Thanks in advance for you help. SQL Server 2000 I have a complex join, but a simple example will ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 03:35 AM
Dave
 
Posts: n/a
Default SQL Server Max Record, multiple table join problems

Thanks in advance for you help.
SQL Server 2000

I have a complex join, but a simple example will do the trick so...

table1 (placement records, possibly many per case,highest ID being
most recent)
----------
placementID(pk) * CaseID(fk) * OrganizationID(fk) * Name * Number

table2 (Organizations Table, Many Placements can be at 1 organization)
----------
OrganizationID(pk) * OrgName * OrgType

table 3(Case Table, each Case can have many placements)
----------
CaseID(pk) * StaffName * CreationDate

now my query...

SELECT t1.placementID,t1.caseID,t2.OrgName
FROM table1 as t1
INNER JOIN table2 as t2 on t2.OrganizationID = t1.OrganizationID
WHERE exists (select distinct max(placementID),CaseID
from t1 group by caseID)
GROUP BY t2.OrgName,t1.PlacementID,t1.CaseID

my results
-------------
placementID CaseID OrgName
1 1 oneOrg
2 1 two org
3 1 three org
4 2 another org
5 3 yet another org

my desired results
------------------
PlacementID CaseID OrgName
3 1 three org
4 2 another org
5 3 yet another org


as you can see i get all records, but what i want is to see only the
last placementID for each case so i dont want duplicate caseID but I
do need the orgName, and yes the query works correctly without the org
name but as soon as i add orgName to the select statement I get
duplicate CaseID's, How do i eliminate duplicate CaseID's and get only
the MAX(placementID) for each Case and the OrgName

please advise, getting desperate.
thanks again so much for the help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:35 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SQL Server Max Record, multiple table join problems

On 22 Apr 2004 17:09:24 -0700, Dave wrote:

>Thanks in advance for you help.
>SQL Server 2000
>
>I have a complex join, but a simple example will do the trick so...
>
>table1 (placement records, possibly many per case,highest ID being
>most recent)
>----------
>placementID(pk) * CaseID(fk) * OrganizationID(fk) * Name * Number
>
>table2 (Organizations Table, Many Placements can be at 1 organization)
>----------
>OrganizationID(pk) * OrgName * OrgType
>
>table 3(Case Table, each Case can have many placements)
>----------
>CaseID(pk) * StaffName * CreationDate
>
>now my query...
>
>SELECT t1.placementID,t1.caseID,t2.OrgName
>FROM table1 as t1
>INNER JOIN table2 as t2 on t2.OrganizationID = t1.OrganizationID
>WHERE exists (select distinct max(placementID),CaseID
> from t1 group by caseID)
>GROUP BY t2.OrgName,t1.PlacementID,t1.CaseID
>
>my results
>-------------
>placementID CaseID OrgName
>1 1 oneOrg
>2 1 two org
>3 1 three org
>4 2 another org
>5 3 yet another org
>
>my desired results
>------------------
>PlacementID CaseID OrgName
>3 1 three org
>4 2 another org
>5 3 yet another org
>
>
>as you can see i get all records, but what i want is to see only the
>last placementID for each case so i dont want duplicate caseID but I
>do need the orgName, and yes the query works correctly without the org
>name but as soon as i add orgName to the select statement I get
>duplicate CaseID's, How do i eliminate duplicate CaseID's and get only
>the MAX(placementID) for each Case and the OrgName
>
>please advise, getting desperate.
>thanks again so much for the help


Hi Dave,

Try this:

SELECT t1.placementID,t1.caseID,t2.OrgName
FROM table1 as t1
INNER JOIN table2 as t2 on t2.OrganizationID = t1.OrganizationID
WHERE t1.placementID = (select max(t1b.placementID)
from table1 as t1b
where t1b.caseID = t1.caseID)

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:57 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com