Results 1 to 10 of 10
  1. #1
    makatak88 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    5

    same length query

    Hi Guys,
    Im a bit of a noob at this and im trying to work out how to show the hoses that have the same length as another one. This is as close as I got which all it shows is which hose lengths appear more than once. I used the Having count >1 because if it doesnt appear at least once it cant have another matching length. I think im thinking to hard about this. Any help would be appreciated.


    cheers


    SELECT hose.length
    FROM hose
    WHERE hose.length=(length)
    GROUP BY length
    HAVING Count(hose.length)>1;

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Try removing the Where line.

    Select hose.length, count(hose.length)
    GROUP BY length
    HAVING Count(hose.length)>1;

  3. #3
    makatak88 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    5
    thnx orange but

    Select hose.length, COUNT(hose.length)
    GROUP BY hose.length
    HAVING Count(hose.length)>1;

    just gives me a syntax error. I see count used on the select line alot and it never works for me. like i said Im a noob and just trying to teach myself this. making slow headway.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Here are a few jpgs. It shows the table with values, the query SQL and the result.
    Where's the syntax error?

  5. #5
    makatak88 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    5
    orange thanks, its amazing some people actually take the time to help people trying to learn. I simply dont really understand subqueries yet (im assuming thats what it is) and didnt have all the brackets around the last line. Your example works great. I just have one more question. if i wanted to retrieve the name of the hose in that query I add hose.name to the select and group by lines but get 0 results.

    Select hose.length, hose.name
    FROM hose
    GROUP BY hose.length, hose.name
    HAVING (((Count(hose.length))>1));

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    First, this is not a subquery. This is an aggregate function

    More info here http://www.techonthenet.com/sql/index.php

    To your query: You first asked about the number of hoses organized by length.

    How do the names of hoses relate to length? You know your business and terminology. The rest of us do not, and can only guess until you give us specifics.

    If each hose, even those with the same name, can be of different lengths, then you have to be very specific in what question(query) you want to ask.

    "How many 10 ft hoses do we have" is a very different question than
    "How many Deluxe_Super Hoses do we have and what are their lengths"

    Do you have a few sample records? And a few sample questions you want answered?

    Some links for learning:

    http://allenbrowne.com/subquery-01.html Subqueries
    http://allenbrowne.com/tips.html Tremendous site - all kinds of goodies

    http://www.sfubusiness.ca/areas/mis/...essons/map.pdf ( lessons)

    http://www.rogersaccesslibrary.com/forum/topic238.html Database design/entity relationship diagrams/hernandez approach

    http://forums.aspfree.com/attachment...achmentid=4712 Great short, easy to read re: RDBMS


    Re: your question - You would get results if you Group By name also and if there were more than 1 hose for that HoseName.

  7. #7
    makatak88 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    5
    hey again orange. Those links are awesome. ive just started to have a look at them.
    I should have included a shot of the table to start with. and I got a little confused with what I was actually trying to achieve. Basically I was trying to get all the names of the hoses that had the same length.
    Using

    Select hose.hose_length
    FROM hose
    GROUP BY hose.hose_length
    HAVING (((Count(hose.hose_length.length))>1));

    returns 7 and 9. but i need to get all the names of the ones that are exactly the same length as the other ones. so I should have 3 of the 7's and their names and 2 of the 9's
    I think ive gone about it the wrong way.
    cheers for youre patience

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    and I got a little confused with what I was actually trying to achieve
    .

    You are not the first to do so. If you read many of the posts on forums, especially for posters with less than(say 40) posts, the question is usually about a from or subform or a button. Rarely have they looked at Database/Table design, normalization, E_R diagramming etc.

    A simple technique that will help in most cases is to write down in a paragraph or so exactly what the problem/issue/opportunity is. Then, on paper,
    look for the nouns, look for the verbs....
    as per http://www.rogersaccesslibrary.com/T...lationship.zip

  9. #9
    makatak88 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    5

    Cool

    those allenbrowne sites are great. i got it sussed thnx for your help

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Quote Originally Posted by makatak88 View Post
    those allenbrowne sites are great. i got it sussed thnx for your help
    Absolutely! An excellent reference for many Access issues.
    The other sites have valuable info also.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Fixed character length of field
    By tylerg11 in forum Access
    Replies: 3
    Last Post: 09-29-2011, 11:58 AM
  2. The length of detail section of a report
    By blueraincoat in forum Reports
    Replies: 2
    Last Post: 04-06-2011, 12:24 AM
  3. Length of Stay
    By lhysell in forum Queries
    Replies: 1
    Last Post: 02-02-2011, 10:45 AM
  4. Sort according to field length?
    By wawinc in forum Queries
    Replies: 4
    Last Post: 12-15-2010, 04:27 PM
  5. Converting a date to length of time
    By Duncan in forum Access
    Replies: 2
    Last Post: 11-10-2010, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums