Results 1 to 5 of 5
  1. #1
    mokje5 is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    10

    Average of movies watched each day query

    Hey, I have the following tables:



    Code:
    ------Customer------
    customer_email_address (PK)
    subscription_start_date
    subscription_end_date
    -----------------------
    
    -----Watchhistory-----
    customer_email_address (FK)
    movie_id
    price
    --------------------------
    Now I would like to make a query that selects the customer_mail_address from Customer and the average of movies watched per day between the subscription_start_date and the current time (so right now).
    A condition is to show only the records of customers that have an average of 2 watched movies per day.

    So the query will look something like this:
    Code:
    SELECT Customer.customer_email_address, (some calculation of movies watched per day here) AS watchedPerDay 
    FROM Customer INNER JOIN Watchhistory ON Customer.customer_email_address = Watchhistory.customer_email_address
    GROUP BY Customer.customer_email_address, (calculation here again?)
    HAVING (2 or more watched movies per day calculation)
    Anyone can help?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    its 2 queries, Q1 get avg
    select customer , AVg(movieID) as AvgRent from table where rentDate between subscripSTart and subscripEnd

    then Q2 uses Q1
    select customer ,AvgRent from Q1 where AvgRent = 2

  3. #3
    mokje5 is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    10
    I think it's 2 queries too, but your solution does unfortunatly not really help me, sorry

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I think the calculations would be along this line:

    A: Number of Days between Today and SubscriptionStartDate

    B: Number of Movies for this Customer

    Avg number of movies watched per day = B/A

  5. #5
    mokje5 is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    10
    Alright, my friend came up with this solution:
    Code:
    SELECT temp.customer_mail_address, Round((Avg(counts)/DateDiff("d",temp.subscription_start,Now())),2) AS average_moviesFROM (SELECT DATEDIFF("d",Customer.subscription_start,NOW()) AS time_overlaps, Watchhistory.customer_mail_address, count(*) AS counts, Customer.subscription_start FROM Watchhistory INNER JOIN Customer ON Watchhistory.customer_mail_address =  Customer.customer_mail_address GROUP BY Watchhistory.customer_mail_address, Customer.[subscription_start])  AS temp
    GROUP BY temp.customer_mail_address, DateDiff("d",temp.subscription_start,Now())
    HAVING (((Avg([counts])/DateDiff("d",[temp].[subscription_start],Now()))>=2));
    This query works, but I can't really understand whats going on here, and neither can my friend :P Maybe someone has an easier variant to this query, or someone can explain?

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

Similar Threads

  1. Replies: 6
    Last Post: 06-15-2014, 07:21 PM
  2. Query to average between two dates.
    By stormchaser in forum Queries
    Replies: 4
    Last Post: 12-15-2011, 03:06 PM
  3. Average Query
    By Lorlai in forum Queries
    Replies: 3
    Last Post: 07-21-2011, 10:11 AM
  4. Replies: 2
    Last Post: 07-14-2010, 06:10 AM
  5. Replies: 13
    Last Post: 05-28-2010, 11:57 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