Two Counts In One Query?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Good Man

    #1

    Two Counts In One Query?

    Hi there

    I'm wrapping up my first *really* big, complex web application, and in
    doing so, I realize that my next tast will be reading some SQL books and
    learning how to improve my querying. I really didn't know that i could
    work math equations, comparisons, and if clauses right in the statements
    themselves.

    Anyways, I digress. I'm looking for a way to query the following... I have
    a database full of people, where the Membership field is either a '1' or
    '0'. I'd like to get the total counts for both situations in one query.
    Something that will give me:

    RegisteredMembe rs | 332
    UnregisteredMem ber | 543


    I can obviously get this result using two queries:
    +
    SELECT COUNT(*) AS RegisteredMembe rs FROM Contacts WHERE Membership='1'
    and
    SELECT COUNT(*) AS UnRegisteredMem bers FROM Contacts WHERE Membership='0'


    but can I do this in one query?

    THANKS!!
  • Bill Karwin

    #2
    Re: Two Counts In One Query?

    Good Man wrote:[color=blue]
    > I can obviously get this result using two queries:
    > +
    > SELECT COUNT(*) AS RegisteredMembe rs FROM Contacts WHERE Membership='1'
    > and
    > SELECT COUNT(*) AS UnRegisteredMem bers FROM Contacts WHERE Membership='0'[/color]

    SELECT Membership, COUNT(*)
    FROM UnRegisteredMem bers
    GROUP BY Membership

    Regards,
    Bill K.

    Comment

    • Good Man

      #3
      Re: Two Counts In One Query?

      Bill Karwin <bill@karwin.co m> wrote in
      news:d8utqc0bhm @enews3.newsguy .com:
      [color=blue]
      > Good Man wrote:[color=green]
      >> I can obviously get this result using two queries:
      >> +
      >> SELECT COUNT(*) AS RegisteredMembe rs FROM Contacts WHERE
      >> Membership='1' and
      >> SELECT COUNT(*) AS UnRegisteredMem bers FROM Contacts WHERE
      >> Membership='0'[/color]
      >
      > SELECT Membership, COUNT(*)
      > FROM UnRegisteredMem bers
      > GROUP BY Membership[/color]

      Thank you Bill.

      I can't wait to sit down with a good SQL book this summer!

      Comment

      Working...