• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    Welcome to the HEXUS.community discussion forums forums.

    You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 11-01-2005, 01:14 PM   #1 (permalink)
    Hmmm bed
     
    Join Date: Jul 2003
    Posts: 427
    Thanks: 0
    Thanked 0 Times in 0 Posts
    SQL Query

    Ok, I have something i just can't get my head round, spent ages trying to get it to work last night but to no avail, so here goes!

    I have a query, I wish to get the query to display the customer firstname and surname and the number of videos they have hired, the tables look like this (i have only included the fields i think we need):

    Customer---

    firstname | surname | memberid

    rental---

    memberid | rentalnumber

    so i was thinking count the number of times the customers memberid appears in *** table and bobs your uncle, well i got it to display the member id along side how many times it occurs:

    select memberid, count(memberid)
    from rentalhead1
    group by memberid

    but when ever i try and link/relate it to the customer table, it does not work as i can not use the surname/firstname columsn where a group by is in there!

    any help would be great,

    cheers
    Basher is offline   Reply With Quote
    Old 11-01-2005, 01:27 PM   #2 (permalink)
    HEXUS.net Webmaster
     
    Iain's Avatar
     
    Join Date: Jul 2003
    Location: UK
    Posts: 3,112
    Thanks: 1
    Thanked 0 Times in 0 Posts
    SELECT c.firstname, c.surname, COUNT(c.memberid) AS num_rentals
    FROM customer AS c, rental AS r
    WHERE r.memberid=c.memberid
    GROUP BY c.memberid

    should do the trick I think

    Iain is offline   Reply With Quote
    Old 11-01-2005, 02:37 PM   #3 (permalink)
    Pixel Abuser
     
    Spunkey's Avatar
     
    Join Date: Nov 2003
    Location: Milton Keynes
    Posts: 1,523
    Thanks: 0
    Thanked 0 Times in 0 Posts
    I assume your version is for MySQL Az as i couldnt get it to work on SQL Server - it wasnt happy without an inner join.

    So if you use MS SQL, heres what you'd need.

    SELECT Customer.FirstName, Customer.Surname, COUNT(Customer.MemberID) AS [Num_Rentals]
    FROM Customer
    INNER JOIN Rental ON Customer.MemberID = Rental.MemberID
    GROUP BY Customer.MemberID
    ORDER BY [Num_Rentals] DESC, Customer.Firstname, Customer.Surname

    Spunkey is offline   Reply With Quote
    Old 11-01-2005, 02:43 PM   #4 (permalink)
    bored out of my tiny mind
     
    malfunction's Avatar
     
    Join Date: Jul 2003
    Location: Berkshire
    Posts: 3,029
    Thanks: 56
    Thanked 58 Times in 50 Posts
    malfunction's system
    IIRC MS SQL requires you to put ALL of the other columns in the group by clause if you don't use the join syntax
    malfunction is offline   Reply With Quote
    Old 11-01-2005, 03:09 PM   #5 (permalink)
    HEXUS.net Webmaster
     
    Iain's Avatar
     
    Join Date: Jul 2003
    Location: UK
    Posts: 3,112
    Thanks: 1
    Thanked 0 Times in 0 Posts
    Yes, mine was MySQL, he hasn't stated what it was so that might be the first answer to help him

    Iain is offline   Reply With Quote
    Old 11-01-2005, 03:13 PM   #6 (permalink)
    Hmmm bed
     
    Join Date: Jul 2003
    Posts: 427
    Thanks: 0
    Thanked 0 Times in 0 Posts
    hey, cheers guys, i'm using Oracle. I got this to work ...

    SELECT C.memberid, C.SurName, C.FirstName, Count(R.memberid) TotalHired
    FROM member1 C
    INNER JOIN rentalhead1 R ON R.MemberId = C.MemberId
    GROUP BY C.MemberId, C.SurName, C.FirstName

    thanks for your help
    Basher is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

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

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


    Similar Threads
    Thread Thread Starter Forum Replies Last Post
    My SQL, circular, inefficient ... or neither? Anders Software and web development 14 17-11-2004 09:27 PM
    Learning SQL Lead_Head Software and web development 13 26-05-2004 05:54 PM
    more VBA and SQL 'problems'... streetster Software and web development 7 07-01-2004 09:45 PM
    SQL help Agent Software and web development 23 11-11-2003 08:22 AM



    All times are GMT. The time now is 11:10 AM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned.
    HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    For more information please read HEXUS Limited's terms, conditions and privacy policy.

    Hosted Exchange

    Powered by vBulletin® Version 3.8.4
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.3.2
    © Copyright 2009 HEXUS® Limited. All rights reserved. Unauthorised reproduction strictly prohibited.