Results 1 to 3 of 3
  1. #1
    swerenfl is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Illinois
    Posts
    2

    MS Access SQL Query - Probably simple, but I'm rusty

    Hello all

    This is my first post, so please be kind. I work with MS products, java, MySQL, PHP, etc all the time, but for the life of me I cannot figure out this semi-basic query. Maybe because it's Monday, maybe because I haven't had enough coffee, or maybe its because I haven't touched SQL in over 6 months. Whatever the case, I need your help. Here is the setup...

    I have a form that people can use to send emails to our clients. The user has an option to select between two canned messages (Message "1" or Message "2"). Behind the scenes, every time they hit the "SEND" button it logs into a "RECORDS" table (so long as it makes it through the error handlers).

    Let's pretend the RECORDS table with 2 columns:

    Code:
        CUST_ID, EMAIL_NUM
        0000, 1
        0000, 2
        0000, 1
        0000, 1
        0001, 2
        0002, 1
        0002, 1
        0003, 2
        0003, 2
        0003, 2
    I need a query that counts the ones and two for each CUST_ID. So the result set should look something like this:



    Code:
        CUST_ID, EMAIL_1_COUNT, EMAIL_2_COUNT
        0000, 3, 1
        0001, 0, 1
        0002, 2, 0
        0003, 0, 3
    I've used count, group bys, havings, while, union, nested selects, but like I said, I'm probably over complicating something that is relatively easy.

    Please help. Thank you!

  2. #2
    swerenfl is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2012
    Location
    Illinois
    Posts
    2
    Answered my own question

    select
    CUST_ID,
    sum(iif(EMAIL_NUM = 1, 1, 0)) as EMAIL_1_COUNT,
    sum(iif(EMAIL_NUM = 2, 1, 0)) as EMAIL_2_COUNT
    from
    RECORDS
    group by
    CUST_ID

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Thanks for posting back with your success and solution.

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

Similar Threads

  1. excel link with access differs from simple export from access
    By M0RDANT in forum Import/Export Data
    Replies: 4
    Last Post: 03-25-2013, 02:43 PM
  2. Rusty
    By ladrexler in forum Forms
    Replies: 0
    Last Post: 03-01-2011, 10:09 PM
  3. Replies: 0
    Last Post: 10-21-2010, 08:24 AM
  4. need help with this simple query
    By dada in forum Programming
    Replies: 3
    Last Post: 08-20-2010, 07:08 AM
  5. simple query
    By taylorosso in forum Queries
    Replies: 1
    Last Post: 10-06-2009, 04:26 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