Re: Count Problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Count counts the existence of a value. Since x.Minus>0 is always going to return a value of true (-1) or False (0) (except in the rare instance the Minus is a null value) you are going to get a count = to the number of records.

Either one of the following expressions should return the count you want

Abs(Sum(x.Minus>0))
Count(IIF(x.Minus>0,X.Minus,Null))

The first works by adding up all the -1 and 0 returned by the expression x.Minus > 0 and then using Abs to strip off the negative sign.

The second works since the IIF returns Null (null aren't counted) or the value of X.minus (non-null values are counted).

Hope this helps

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County

Dawn wrote:
Table x has only one field “minus”, whose value from (-∞,+∞).
Query as below:
Select count(x.minus>0) as field1,
Sum(iif(x.minus>0,minus,0)) as field2
From x;
But after running, the result of field2 is correct, while field1 is the total records number of table x, not what I wanted.

As One of the MVP recommended in a similar problem, he used function: “abs”,
Query:
Select abs(x.minus>0) as field1,
Sum(iif(x.minus>0,minus,0)) as field2
From x;

When running , it occurs run-time error.

What I want is to calculate the sum of minus which is over zero, and the respective records number.

Thanks.
Dawn
.



Relevant Pages

  • Re: Group By, Max and Min
    ... SELECT field1, field2, field3, LAST, MIN, MAX ... I have (from a previous saved query) these fields: ...
    (microsoft.public.access.queries)
  • Re: Using flexible query criteria
    ... criteria for the query is based on the user's input in the main form. ... One of the user inputs is a field called "Vendor_Name" (in the form of ... and said that Field1 is the primary key and is an autonumber field, and Field2 is the field shown in the combo box, you must modify it to a UNION select, similar to ...
    (comp.databases.ms-access)
  • Re: need to delete record having two different fields with same identical value
    ... Assuming you're always comparing the same two fields (Field1 and Field2) ... Run the query to check it, and then turn it into a delete query. ... which might be a lot safer (I hate deleting data unless I ...
    (microsoft.public.access.gettingstarted)
  • Re: Query problem - How do I do this?
    ... You may be able to normalize your table with a union query ... SELECT 1 as Fld, Field1 as TheValue, Countas NumOf ... FROM tblTooManyFields ... SELECT 2, Field2, Count ...
    (microsoft.public.access.queries)
  • Re: Column Field to Row Field
    ... Duane Hookom ... MS Access MVP ... Field2 has from one to many results for Field1. ...
    (microsoft.public.access.queries)