Results 1 to 6 of 6

Thread: SQL Query

  1. #1
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    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

  2. #2
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    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

  3. #3
    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

  4. #4
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    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

  5. #5
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    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

  6. #6
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My SQL, circular, inefficient ... or neither?
    By Anders in forum Software
    Replies: 14
    Last Post: 17-11-2004, 10:27 PM
  2. Learning SQL
    By Lead_Head in forum Software
    Replies: 13
    Last Post: 26-05-2004, 06:54 PM
  3. more VBA and SQL 'problems'...
    By streetster in forum Software
    Replies: 7
    Last Post: 07-01-2004, 10:45 PM
  4. SQL help
    By Agent in forum Software
    Replies: 23
    Last Post: 11-11-2003, 09:22 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
  •