?
Solved

SQL Server 2005 Join Table on pk and displayorder field

Posted on 2008-06-15
5
Medium Priority
?
301 Views
Last Modified: 2010-04-21
Hi,
I have two tables Listing and ListingPictures

What I would like to do is query these tables for listing data plus the listing picture with the lowest display order. Fields and sample data below:

Listing Fields:
ListingID | StreetAddress | City | State | Zip | etc

ListingPictures Fields
ListingPictureID | ListingID | FileName | DisplayOrder

Sample Listing Data
17 | 12345 Some Street West | Miami | Florida | 33161
18 | 23456 Some Street East | West Palm Beach | Florida | 33151

Sample ListingPictures Data:
29 | 17 | ListingPictures/12345.jpg | 1
30 | 17 | ListingPictures/23456.jpg | 2
31 | 18 | ListingPictures/34567.jpg | 1
32 | 18 | ListingPictures/45678.jpg | 2

So the ideal query for me would return results like below
ListingID | ListingPictureID | ListingPictureFileName      | StreetAddress | City        | State        | Zip
17          | 29                      | ListingPictures/12345.jpg | The Address    | The City | The State | The Zip
18          | 31                      | ListingPictures/34567.jpg | The Address    | The City | The State | The Zip

If anyone could help with that query I would greatly appreciate it.
Thanks in advance!
0
Comment
Question by:amagondes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:darkmooink
ID: 21789854
select * from Listing join ListingPictures on ListingPictures.ListingID = Listing.ListingID where DisplayOrder= 1
0
 

Author Comment

by:amagondes
ID: 21790446
Hi darkmooink,
thanks for the reply. the problem is that there is not always guaranteed to be a display order of 1. would it be possible to do something like:

SELECT Listing.*, ListingPicture.ListingPictureID, ListingPicture.FileName FROM Listing
LEFT JOIN ListingPicture ON ListingPicture.ListingID = Listing.ListingID
WHERE ListingPicture.DisplayOrder = (SELECT MIN(ListingPicture.DisplayOrder) FROM ListingPictures WHERE ListingPicture.ListingID = Listing.ListingID  )

This seems to work, but is there a more efficient way to write this query?

0
 

Author Comment

by:amagondes
ID: 21790465
Actually, if there are no pictures for a listing the listing won't get returned at all. i would like it to return listings whether they have a picture(s) or not, but if they do have a picture then return the picture with the lowest display order. thanks for the help
0
 
LVL 3

Accepted Solution

by:
darkmooink earned 2000 total points
ID: 21792848
as for the not displaying of the record if there isnt a record on listingPicture you could add " or ListingPicture.DisplayOrder is null" to the end of the query that seems to work for some data i have.
but streamlining it i dont think i can help. i have tryed a few things but i eather dont get the results i want or i error.
0
 

Author Closing Comment

by:amagondes
ID: 31467407
Thanks for your help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question