Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat SQL queries will help analyse a membership base?
    primarykey
    data
    text
    <p>At our sports centre, I would like to analyse the number and types of subscriptions our members have, but I'm having trouble with the exact SQL queries.</p> <p>A "subscription" is an entry pass that lets you into specific activities in our sports centre. They have a start and end date, representing the period they are valid. They have an associated activity (eg. "gym", "swim", "group fitness", "rock climbing", etc.)</p> <p>A member can have more than one subscription. Typically, one will expire before they purchase their next one, but they can have two (or more) valid (or current) subscriptions.</p> <p>Simplifying a little, we have the following two tables:</p> <pre> Members - stores a member's data, has these columns ID (int) - unique ID for a member Name (varchar) - the member's name Gender (varchar) - the member's gender NumGym (int) - number of valid gym passes a member has (derived) </pre> <p>and</p> <pre> Subscriptions - holds all the subscriptions we've sold, has these columns TypeID (varchar) - unique ID for a subscription type (specifies activity) MemberID (int) - the ID of the member that purchased this subscription StartDate (datetime) - when the subscription is valid from EndDate (datetime) - when the subscription is valid to </pre> <p>Some of the questions I would like to be able to ask are:</p> <ol> <li>how many members currently have a valid "gym" pass?</li> <li>how many active members do I have on a given day</li> </ol> <p>For (1), I've tried the following:</p> <pre><code>declare @aDay datetime set @aDay = convert(datetime, '2009-12-08', 102) update Members m set NumGym = (select count(memberid) from Subscriptions s where s.MemberId = m.Id and s.TypeID = "Gym" and @aDay between s.StartDate and s.EndDate) </code></pre> <p>However it doesn't seem to be producing the correct results (based on our understanding of our membership and some manual counting).</p> <p>Not having been able to get (1) to work, I haven't tried (2) yet.</p> <p>Any suggestions on queries to produce what I'm after?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload