Results 1 to 5 of 5
  1. #1
    oriels is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    4

    Query - Group by, Count if ,Create a new field, If>x,"Good","Bad"

    Hello


    I have a table like this:
    Customer ID, Order ID, Date.
    (It can contain two rows or more with the same customer ID)

    So I need a query that will do that:
    If customer made more then 2 orders, in July 15 (for example), then write in a new cell "Good" or "Bad".

    I succeed to do it with 3 queries. But, I think it possible with 2 queries or even to do it with one query. Does anyone have any idea how to do it?

    The way i did it with 3 queries:

    First query:"1" (filter for get july orders)
    Customer ID
    Order Date - criteria (range of July 15).

    Second query:"2" (sort by a group per customer and count the orders in that date)
    Customer ID - Group by. - query 1
    Order Date - count - query 1.

    Third query :"3" ( actual check )
    Customer ID - query 2
    Count - (just for display reasons -- not
    necessary)
    Good or Bad: if query 2 date count is more then 2 so "good" else "bad".

    ===
    I thought to make "if" term in query 2 but it not worked for me.
    pleas i need it for homework.

    Thank you all!

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    query 1

    SELECT CustomerID, count(*) As OrderCount
    FROM myTable
    WHERE month(mydate)=7 and year(mydate)=2015
    GROUP BY CustomerID

    query 2

    SELECT *, iif(ordercount>1,"Good","Bad") as Status
    FROM Query1

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Should not use spaces, punctuation or special characters (underscore is the exception) in object names.

    "Date" is a reserved word in Access and shouldn't be used as an object name.

  4. #4
    oriels is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    4
    Quote Originally Posted by ssanfu View Post
    Should not use spaces, punctuation or special characters (underscore is the exception) in object names.

    "Date" is a reserved word in Access and shouldn't be used as an object name.
    thank you .!
    Quote Originally Posted by Ajax View Post
    query 1

    SELECT CustomerID, count(*) As OrderCount
    FROM myTable
    WHERE month(mydate)=7 and year(mydate)=2015
    GROUP BY CustomerID

    query 2

    SELECT *, iif(ordercount>1,"Good","Bad") as Status
    FROM Query1
    In Query 1 - I have just (*) no "Count(*)" in "myTable" do i need to write it? can you sxplain me how to do it?
    and also if i make "total" it not allow me to "gruop by".

    Ok I got it and it worked!!!

    Thank you very very much!

  5. #5
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    In Query 1 - I have just (*) no "Count(*)" in "myTable" do i need to write it? can you sxplain me how to do it?
    most help for queries uses sql as examples etc.

    Using the query designer generates sql and this can be seen by going to the sql window which can be found in the dropdown on the left of the ribbon.

    for future questions you may have, copy and paste this code into your post (no screenshots) - it tells us a lot more than you describing it. It also means that responders will usually use your table and field names so you can simply copy and paste back and return the query design window to see what it looks like. This makes it easier for everyone. The number of times the OP retypes the code manually, makes a mistake and then says 'it doesn't work' is very frustrating

    I fully agree with ssanfu - never understood why users use spaces - means you have 3 extra characters to type each time and increased opportunity for errors. That is what the caption property is for

    here is a link to access reserved words https://support.office.com/en-us/art...7-da237c63eabe

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

Similar Threads

  1. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  2. Replies: 1
    Last Post: 07-10-2015, 06:33 AM
  3. Replies: 1
    Last Post: 02-05-2015, 05:41 PM
  4. Replies: 16
    Last Post: 07-22-2011, 09:23 AM
  5. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 PM

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