help with SubQuery

I have two database tables, AdvertImages (imageid, AdvertInfoid, Imageurl) and Advertinfo (advertinfoid, AdvertType,  Title, Description, Price) The table Advertimages can hold as many images as wanted for each advert.

What I want to do is create a query that allows me to show the 1st image for each advert along with the AdvertType, Name and price . I thought I could do this with a subquery but I'm not getting anywhere. The two tables are joined on advertinfo.advertinfoid = advertimages.imageid

Can someone help please?
markejAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
They shouldn't be joined by advertinfo.advertinfoid = advertimages.advertinfoid?

SELECT Advertinfo.advertinfoid, MIN(Advertimages.imageid)
FROM Advertinfo 
INNER JOIN Advertimages ON advertinfo.advertinfoid = advertimages.advertinfoid
GROUP BY Advertinfo.advertinfoid

Open in new window

0
Shaun KlineLead Software EngineerCommented:
Something like this?
select advertinfo.AdvertType, advertinfo.Title, advertinfo.Price, advertimages.ImageUrl
from AdvertInfo 
  inner join AdvertImages N advertinfo.advertinfoid = advertimages.imageid
  inner join (select AdvertInfoid, MIN(imageid) ImageID from advertImages GROUP BY AdvertInfoid) FirstImage
        ON AdvertImages.AdvertInfoid = FirstImage.AdvertInfoid AND AdvertImages.ImageID = FirstImage.ImageID 

Open in new window

0
markejAuthor Commented:
Great help and quick thanks gents and  Vitor Montalvão was correct the join is on advertinfo.advertinfoid = advertimages.advertinfoid, sorry my apologies.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

markejAuthor Commented:
Sorry Gents, just realised this isn't quite correct both your solutions return multiple copies of the same record one for each image added, I've left of the imageurl for clarity


i.e.
advertinfo.id AdvertType Title Price
1      Standard      test air gun      56.78      
26      Standard      image test      40.00      
27      Standard      image test      0.00
2      Premium      gun      34567.99      
28      Standard            0.00      
2      Premium      gun      34567.99      
2      Premium      gun      34567.99
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Anyway, my query can't return duplicate records since is grouped by ID so can only return a single ID.
0
Shaun KlineLead Software EngineerCommented:
If you post you updated query, that would help.
0
markejAuthor Commented:
I have attached a spreadsheet containing the my test data from the two tables, the first table is the advertimages table and has the fields imgid, advertinfoid and thumb, the second table is the advertinfo table and has the fields id, adverttype, title, description, price and dateadded (this is a subset of all the fields) BUT these are the fields I want in the query,  additionally  I would like to sort on dateadded.

The two tables are linked by advertimages.advertinfoid = advertinfo.id

Instead of seeing this data:
advertinfo.id AdvertType Title Price thumb
1      Standard      test air gun      56.78      a
26      Standard      image test      40.00      b
27      Standard      image test      0.00 c
2      Premium      gun      34567.99      d
28      Standard            0.00      e
2      Premium      gun      34567.99       f
2      Premium      gun      34567.99 g

What I'm after in the query is one record for each entry in the Advertinfo table with the first image  from the possibly multiple images in the advertimages table

i.e.
advertinfo.id AdvertType Title Price Thumb
1      Standard      test air gun      56.78      a
26      Standard      image test      40.00      b
27      Standard      image test      0.00 c
2      Premium      gun      34567.99      d
28      Standard            0.00     e

I hope that explains the problem

Mark
Book1.xls
0
Shaun KlineLead Software EngineerCommented:
Try this (this is the same query posted previously, but with the join corrected and typo fixed):
select advertinfo.AdvertType, advertinfo.Title, advertinfo.Price, advertimages.Thumb
from AdvertInfo 
  inner join AdvertImages ON advertinfo.id= advertimages.advertinfoid 
  inner join (select AdvertInfoid, MIN(imgid) ImageID from advertImages GROUP BY AdvertInfoid) FirstImage
        ON AdvertImages.AdvertInfoid = FirstImage.AdvertInfoid AND AdvertImages.ImgID = FirstImage.ImageID 

Open in new window

You can test this individually by running the subquery, and seeing that you get one row per AdventInfoID:
select AdvertInfoid, MIN(imgid) ImageID from advertImages GROUP BY AdvertInfoid

Open in new window

Then run the larger query:
select AdvertImages.*
from AdvertImages
  inner join (select AdvertInfoid, MIN(imgid) ImageID from advertImages GROUP BY AdvertInfoid) FirstImage
        ON AdvertImages.AdvertInfoid = FirstImage.AdvertInfoid AND AdvertImages.ImgID = FirstImage.ImageID 

Open in new window

Finally run the top query.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
markejAuthor Commented:
Thats it thank you, all working as anticipated
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.