Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecet DISTINCT plus one more

Status
Not open for further replies.

flyadvertising

Programmer
Feb 5, 2011
8
US
The following is working great, but I need to get one more row from the second table (ppics). However, when I try to add this in, my query stops working. Any help will be much appreciated.

SELECT ppre.cID, ppre.yr, ppre.make, ppre.model, ppre.vin, ppre.display FROM ppre
JOIN (SELECT DISTINCT pcID FROM ppics) AS TR
ON TR.pcID = ppre.cID
 
oh, you mean an extra column, not row :)
Code:
SELECT ppre.cID
     , ppre.yr
     , ppre.make
     , ppre.model
     , ppre.vin
     , ppre.display 
     , [red]TR.purl[/red]
  FROM ppre
INNER
  JOIN ( SELECT DISTINCT 
                pcID 
              , [red]purl[/red]
           FROM ppics ) AS TR 
    ON TR.pcID = ppre.cID

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Right, an extra column.

However, the SQL you provided shows all the results (not right). My original statement showed just the distinct TR.pcID's (right), but I am just trying to pull in the associated value from the ppics.purl in the same row as the TR.pcID. Sorry if I am not being clear.
 
This did it:

SELECT ppre.cID, ppre.yr, ppre.make, ppre.model, ppre.vin, ppre.display, TR.purl FROM ppre INNER JOIN ( SELECT DISTINCT pcID, purl FROM ppics ) AS TR ON TR.pcID = ppre.cID GROUP BY ppre.cID

Thank you
 
but I am just trying to pull in the associated value from the ppics.purl

if there was only one purl for each pcID in ppics, then you wouldn't have to do the GROUP BY in the outer query

and since you do have to, that means that there are multiple purl values for each ppre.cID

which means when you GROUP BY BY ppre.cID in the outer query, without applying an aggregate function to purl, you get an indeterminate value for purl -- it could be any one of the values associated with that cID

this is explained here: GROUP BY and HAVING with Hidden Columns

does that help?

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
and of course in ANSI SQL you are not allowed to omit columns from the GROUP BY clause like you can in mysql

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Yes, that helps, thanks. Fortunately, pulling any of the purl values associated with that cID satisfies what I need to do. However, I will look at the aggregate function so I can make the query more accurate.

Again, thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top