Avatar of attipa
attipaFlag for United States of America

asked on 

Having multiple outputs for INNER JOIN

i have two tables: PGImage and PGGallery

PGGallery has the following columns: GalleryID, Image1ID, Image2ID, Image3ID, Image4ID, Image5ID

PGImage has the following columsn: ImageID, Headline, Description, URL


I want to have a single stored procedure to grab the first GalleryID from PGGallery and select all the elements of EACH of the 5 ImageID's

How can I do this?
Microsoft SQL Server

Avatar of undefined
Last Comment
Anthony Perkins
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

>>grab the first GalleryID from PGGallery<<
Please define "first".  Do you have a date column?
Avatar of attipa
attipa
Flag of United States of America image

ASKER

yes.... the stored procedure will start off with Select TOP 1....

and end with ORDER BY PGGallery.DateAdded DESC....
If it is the smallest GalleryID (perhaps it is an IDENTITY column) try something like this:

Select Top 1  *
From PGGallery g
          Inner Join PGImage i1 On g.Image1ID = i1.ImageID
          Inner Join PGImage i2 On g.Image1ID = i2.ImageID
          Inner Join PGImage i3 On g.Image1ID = i3.ImageID
          Inner Join PGImage i4 On g.Image1ID = i4.ImageID
          Inner Join PGImage i5 On g.Image1ID = i5.ImageID
Order By g.GalleryID
Than try it like this:

Select Top 1  *
From PGGallery g
          Inner Join PGImage i1 On g.Image1ID = i1.ImageID
          Inner Join PGImage i2 On g.Image1ID = i2.ImageID
          Inner Join PGImage i3 On g.Image1ID = i3.ImageID
          Inner Join PGImage i4 On g.Image1ID = i4.ImageID
          Inner Join PGImage i5 On g.Image1ID = i5.ImageID
Order By g.DateAdded                     -- If you use DESC you will get the last, not the first
Oops that is not correct, it should be:
Select Top 1  *
From PGGallery g
          Inner Join PGImage i1 On g.Image1ID = i1.ImageID
          Inner Join PGImage i2 On g.Image2ID = i2.ImageID
          Inner Join PGImage i3 On g.Image3ID = i3.ImageID
          Inner Join PGImage i4 On g.Image4ID = i4.ImageID
          Inner Join PGImage i5 On g.Image5ID = i5.ImageID
Order By g.DateAdded
Avatar of attipa
attipa
Flag of United States of America image

ASKER

i think you are on the right track however what the above is producing is the PGGallery Info.  I need the PGImage info for each of the PGGallery Images...

The TOP 1 is only for the PGGallery....so after it selects the TOP 1 PGGallery, it then shows 5 rows of PGImage info (1 row for each PGGallery.Image*ID)....does this make more sense?
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of attipa
attipa
Flag of United States of America image

ASKER

ok, forget putting it on 1 row.  all i want is the ImageID's grabbed from the latest PGGallery row.  is this possible?  i am assuming that each PGImage will have all its information on its own row.
I am not following you. Try posting some sample data and the output desired.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo