Monday, March 12, 2012

Need Query Help

Hello Everybody,

I have the following two tables and I need some help crafting the apropriate SQL Query to retrieve the information that I need.

Table: ImageGalleries

GalleryID INT IDENTITY(1,1),
GalleryName VARCHAR(255),
DisplayOrder INT NOT NULL

Table: ImageDetails

ImageId INT IDENTITY(1,1),
GalleryID INT NOT NULL (FKey relationship to above table)
ImageName VARCHAR(255),
DisplayOrder INT NOT NULL

In the ImageGalleries table and ImageDetails table, DisplayOrder is a positive integer value that resents the order on the page in which the respective entries display

Here's my attempted query: I need to pull the ImageGalleries.GalleryID, ImageGalleries.GalleryName, and the corresponding ImageDetails.ImageID for the image with a DisplayOrder of 1 and order the full result set by ImageGalleries.DisplayOrder

I am sure there is a way to form this query but at the moment it is alluding me. I doubt that a lot of people are looking at the message boards on a holiday weekend but just in case I thought I would post this


SELECT
g.GalleryID,
g.GalleryName,
i.ImageId,
i.ImageName
FROM
ImageGalleries g
INNER JOIN ImageDetails i
ON g.GalleryID = i.GalleryID

ORDER BY
g.DisplayOrder,
i.DisplayOrder

one hour turn around time on a holiday season.. I think that's impressive. :)|||1 Hour turn around is indeed impressive on a Holiday. I shall share my condolences that we were both doing work on a Holiday. As far as the query above, thank you for the effort but it didn't quite suit what I was looking for. I just want to return 1 record per image gallery and that of course returns all records in the image gallery. I thought about doing a sub query

select G.GalleryID, G.GalleryName, Q.ImageId, Q.ImageName FROM ImageGalleries LEFT JOIN (SELECT * FROM ImageDetails WHERE DisplayOrder = 1) AS Q ON G.GalleryID = Q.GalleryID ORDER BY G.GalleryID

But instead, I chose to implement the same functionality in a user defined function cuz I never get to use them at my day job.

select *, dbo.GetFirstImage(GalleryID) As ImageName FROM ImageGalleries|||add..


WHERE
q.DisplayOrder = 1

:) oh well.|||Bloody Hell, that was so obvious that I almost want to cry. Thank you. I will go hide my head in shame now.|||naw man. we all make the most obvious mistakes, and they're impossible to find because you never care to look in that direction.

We all hit that wall every other day. don't stress yourself out like that.

No comments:

Post a Comment