Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Need Query Help (with a COUNT(*) for listings) please..

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-04, 14:05
freekMEDIA freekMEDIA is offline
Registered User
 
Join Date: Jan 2004
Location: Florida
Posts: 25
Angry Need Query Help (with a COUNT(*) for listings) please..

Hey all, I have a problem and it's really making my head spin. If someone could help it would be SO appreciated. I am using an Access DB.

I have the following tables:

listings
- listingID
- cityID (foreign key)
- username (foreign key)
- description
- etc.

listingtypes
- listingtypeID
- listingtype

listingtypelink (linking table for many-many relationship between listings and listingtypes)
- listingID (foreign key)
- listingtypeID (foreign key)

cities
- cityID
- city
- stateID (foreign key)

states
- stateID
- state

members
- username
- password
- etc.

Now my problem, I need to do a SELECT wherein I can select unique city and city ID from the cities table, as well as a COUNT() of the number of listings for each of the cities.

Ideally, results would look like this if I selected the cities that had listings that were of type X (from the listingtypes table, listingtype can be more than one value too such as 12,34,45,46):
Fort Myers (10)
Port Charlotte (1)
Naples (13)
Tampa (26)
etc...

(In other words, I need it to be able to "SELECT DISTINCT cityID, city, numberOfTotalListings FROM listings WHERE cityID = [a single value, such as 233] AND [the listing is active and not disabled] AND [the listing type is either 23 or 45 or 56 or 57 or even a combination of any of these]")

I can't get the count to come out right, no matter what I do, and I just think I am missing something due to my lack of experience with these types of queries.

I have tried nested selects to return a value, which I have had success with in the past, and I know in some way this is the method to use, but I can't figure out the proper query:
strSelect = "SELECT cities.cityID, cities.city, (SELECT COUNT(*) FROM listings WHERE listings.cityID = cities.cityID) AS listingNumber FROM (((cities INNER JOIN listings ON cities.cityID = listings.cityID) INNER JOIN states ON cities.stateID = states.stateID) INNER JOIN members ON listings.username = members.username) LEFT JOIN listingtypelink ON listings.listingID = listingtypelink.listingID WHERE states.stateID = @stateID AND listings.listingdisabled = False AND members.accountdisabled = False AND listingtypelink.listingtypeID IN (" & objListingType & ")

I can't get the count to come out right. Can someone help me to shed some light on this, please? I would be forever grateful...

My problem seems to lie in the fact that there can be more than one listing type at a time (i.e. - 2,4,5,6,7,20) but I still need the distinct count for the cities, which I can't seem to figure out.

Thank you so much!
__________________
--

Travis Nelson
http://www.travis-nelson.com/
Reply With Quote
  #2 (permalink)  
Old 10-01-04, 15:27
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 41
use group by function
SELECT cityID, city, count(*) FROM listings group by cityID,city
Reply With Quote
  #3 (permalink)  
Old 10-01-04, 15:52
freekMEDIA freekMEDIA is offline
Registered User
 
Join Date: Jan 2004
Location: Florida
Posts: 25
Unhappy Oops

Sorry, my bad, I forgot to add the GROUP BY to my query in my post, I am already using the GROUP BY clause in my statement exactly as you stated (group by cityID, city)

I just can't seem to get it to grab the correct records and count.

This probably sounds really lame/bad, but if someone has the time, could they write out a of what I need if they know what it might be? I'm normally pretty good at this, but I've been on this simple little problem for 2 days and I am still just as stuck as ever, I just don't know what else to do.... =(

Thank you guys... very much.
__________________
--

Travis Nelson
http://www.travis-nelson.com/
Reply With Quote
  #4 (permalink)  
Old 10-01-04, 16:12
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,570
How about:
Code:
SELECT Count(*), cities.city FROM listings JOIN cities ON (cities.cityID = listings.cityID) WHERE EXISTS (SELECT * FROM listingtypelink WHERE listingtypelink.listingID = listings.listingID AND listingtypeID IN (12,34,45,46))
-PatP
Reply With Quote
  #5 (permalink)  
Old 10-01-04, 16:32
freekMEDIA freekMEDIA is offline
Registered User
 
Join Date: Jan 2004
Location: Florida
Posts: 25
Talking It Worked!!!

THANK YOU SO MUCH!!

You have no idea how awesome it was for you to take the time to write that out for me, I just copied and pasted it, and made a couple small tweaks and it worked perfectly!

Did not know I could use WHERE EXISTS, will have to look into this more.

THANK YOU THANK YOU THANK YOU!!!! =)
__________________
--

Travis Nelson
http://www.travis-nelson.com/
Reply With Quote
  #6 (permalink)  
Old 10-01-04, 17:16
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,570
I'm just curious, but what did you need to tweak?

I couldn't test the code that I posted because I didn't have your MDB file, but nothing jumps out at me as being tweakable. I'd like to know if I missed something so that I get it right next time.

-PatP
Reply With Quote
  #7 (permalink)  
Old 10-01-04, 17:21
freekMEDIA freekMEDIA is offline
Registered User
 
Join Date: Jan 2004
Location: Florida
Posts: 25
Thumbs up Tweakages

Sorry, when I said tweak, I just kinda meant pasting it and then taking out the line breaks, and changing the numbers to a variable. Below is my finished code (In ASP.NET VB)
It's a little bit sloppy, but it works and at this point, that's all that matters. =)

'***********************************

strSelect = "SELECT COUNT(*) AS listingNumber, cities.city, cities.cityID FROM (listings INNER JOIN cities ON cities.cityID = listings.cityID) WHERE EXISTS (SELECT * FROM listingtypelink WHERE listingtypelink.listingID = listings.listingID"

If objListingType <> "all" Then
strSelect &= " AND listingtypelink.listingtypeID IN (" & objListingType & ")"
End If
strSelect &= ") AND cities.stateID = @stateID"
strSelect &= " GROUP BY cities.cityID, cities.city"
strSelect &= " ORDER BY cities.city ASC"

'***********************************

Thanks again!
__________________
--

Travis Nelson
http://www.travis-nelson.com/
Reply With Quote
  #8 (permalink)  
Old 10-01-04, 17:36
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,570
Ah, I see. I was trying to figure out what I might have broken somehow, and you were talking about the cleanup needed to make it work with your application in VB. Now we're on the same page again!

-PatP
Reply With Quote
  #9 (permalink)  
Old 10-01-04, 17:47
freekMEDIA freekMEDIA is offline
Registered User
 
Join Date: Jan 2004
Location: Florida
Posts: 25
Smile Thanks again

hah yeah for sure, sorry, sometimes I just type and don't even really pay attention to what I'm saying (that's the worst part about being a fast typer!)

Thanks so much again, I have since taken that snippet of code and used it on a few other pages for the site. Provided for much optimization as well with only single calls to the database (don't even ask what I was doing before, haha, bad practices for sure.)

Take care.
__________________
--

Travis Nelson
http://www.travis-nelson.com/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On