Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Joining Multiple matches from Right Table to 1 row of results


Joining Multiple matches from Right Table to 1 row of results

Author
Message
eric.kelley
eric.kelley
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
I have a cross reference table, attributexref, which holds values of different fields based distinguished by hattributename. I am wanting to populate a single row for each property record, and have columns for each of the different attribute types from attributexref. Instead it is populating a separate row for each match in the attributexref table. Any ideas?

Here is the code I currently have:


Select distinct
p.scode,
unit.scode,
case av.hattributename when 7 then av.sValue Else '' end,
p.sAddr2
, p.sCity
, p.sState
, p.sZipCode
, case av.hattributename when 8 then av.sValue Else '' end
, case av.hattributename when 34 then av.sValue Else '' end
, PROPBUT8.DUPSTATUS
, case av.hattributename when 5 then av.sValue Else '' end
, PROPBUT13.ACQUISITIONDATE
, PROPBUT13.ACQUISITIONPRICE
, PROPBUT13.ALLINACTUAL
, PROPBUT13.ALLINSTABLE
, PROPBUT13.STABLEDT
, tenant.sRent
, Case tenant.iStatus When 0 Then 'Current' When 1 Then 'Past' When 2 Then 'Future' When 3 Then 'Eviction' When 4 Then 'Notice' When 5 Then 'Vacant' When 6 Then 'Applicant' When 7 Then 'Cancelled' When 8 Then 'Wait list' When 9 Then 'Denied' Else ' ' End
, PROPBUT8.CITYTAX
, PROPBUT8.COUNTYTAX
, PROPBUT8.REFINANCEDSTATUS
, PROPBUT8.DATEREFINANCED
, PROPBUT8.FINANCELENDER
From Property p
Inner Join unit ON p.hMy = unit.hProperty
Left Outer Join PROPBUT13 ON p.hMy = PROPBUT13.hCode
Left Outer Join PROPBUT8 ON p.hMy = PROPBUT8.hCode
Inner Join Tenant ON p.hMy = tenant.hProperty
inner join attributexref attr on p.hmy = attr.hFileRecord and attr.iFileType = 3
inner join attributevalue av on attr.hattributevalue = av.hmy
Where 1= 1



Here are the results I'm currently getting:
00051201   00051201      5 Rolling Hill Ct   Columbia   SC   29172         NULL      NULL   NULL   NULL   NULL   NULL   650.00   Current   NULL   NULL   NULL   NULL   NULL
00051201   00051201      5 Rolling Hill Ct   Columbia   SC   29172         NULL   Bulk Buy   NULL   NULL   NULL   NULL   NULL   650.00   Current   NULL   NULL   NULL   NULL   NULL
00051201   00051201      5 Rolling Hill Ct   Columbia   SC   29172   Oct      NULL      NULL   NULL   NULL   NULL   NULL   650.00   Current   NULL   NULL   NULL   NULL   NULL
00051201   00051201   BLTREJV3 Columbia LLC   5 Rolling Hill Ct   Columbia   SC   29172         NULL      NULL   NULL   NULL   NULL   NULL   650.00   Current   NULL   NULL   NULL   NULL   NULL
aaron.reese
aaron.reese
Mr or Mrs. 500
Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)

Group: General Forum Members
Points: 589 Visits: 898
Welcome to the world of pain that is EAV tables. Entity-Attribute-Value seems like a neat way to get round storing properties of entities until you try to retrieve the data.:-D

The short answer is that there isn't a good way becuase it is (normally) a bad way to organise the data. There is a very good thread on the board somewhere called EAV Vs SQLVariant Vs. XML

Effectively when you want to query the data you need to treat each attribute as if it was a separate table, so why not just store the data like that in the first place:XML if ideally suited for storing attribute values but it does mean that you need to learn XQuery or the XML shredding tools built into your front end language (.NET has great support for XML or so I am told)
Brandie Tarvin
Brandie Tarvin
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7745 Visits: 8719
Actually, I have a few thoughts that could get around this. Mainly via the use of a subquery that selects each distinct property and then JOINs to the other tables to pull up the remaining fields.

Post your Table DDL and some sample data and I'll see what I can whip up that will help you out.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search