Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with SQL query
#1
It's been a while since I've done much of any SQL, I want to extract county code from county table and add it to results of places query where the CountyNames match. Here's my attempt which doesn't work:

SELECT (StateCd, CountyName, PlaceCd, StateAbbr, PlaceName, PlaceType, Fstat) FROM places (CountyCd, CountyName) from county where county.CountyName = places.CountyName order by StateCd, CountyCd, PlaceCd;
I think it's close to this:
SELECT StateCd, CountyName, PlaceCd, StateAbbr, PlaceName, PlaceType, Fstat  FROM places 
INNER JOIN  county ON places.StateCd = county.StateCd AND places.CountyName = county.CountyName
order by StateCd, CountyCd, PlaceCd;
but I get syntax error on = don't know which one
Reply
#2
What about using?

SELECT StateCd, CountyName, PlaceCd, StateAbbr, PlaceName, PlaceType, Fstat  FROM places 
INNER JOIN  county USING (StateCd, CountyName)
order by StateCd, CountyCd, PlaceCd;
Or have you tried parentheses around the condition?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
What SQL are you using? The dialects have incompatibilities.
Reply
#4
Ichabod's join is almost there, there one column that's in both tables, so it barfs on StateAbbr,

Error:
Error: ambiguous column name: StateAbbr
Micseydel - This app uses sqlite3.

This is what the county table looks like:
CountyCd, StateCd, CountyName, StateAbbr, FipsClassCd
Reply
#5
I also tried:

sqlite> SELECT StateCd, CountyName, PlaceCd, StateAbbr, PlaceName, PlaceType, Fstat  FROM places
   ...> LEFT JOIN county ON (places.StateCd = county.StateCd, places.CountyName = county.CountyName);
Error: ambiguous column name: StateCd
different column, same error
Reply
#6
I think you need to specify from which table comes each column

SELECT p.StateCd, p.CountyName, c.CountyCd, p.PlaceCd, p.StateAbbr, p.PlaceName, p.PlaceType, p.Fstat
FROM places AS p
LEFT JOIN county AS c
ON p.StateCd = c.StateCd AND p.CountyName = c.CountyName;
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Buran - You got it!

I used to write some god awful Oracle queries, but it's been years since I did anything other that simple queries.
A Join is simple, but it's like trying to get on the green in two when you haven't golfed in ten years!

Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020