tags:

views:

448

answers:

4

Using Access 2003

Table

EmpID Value1 Value2

001 100 
001       300
001 200   400
001 300   
001       250
001 200   400
002       100   
002 100   200
003 500   100
003
003 700
003

So on…,

I want to count (value1) and Count (value2) is not equal to null.

Expected Output.

EmpID Value1 Value2

001     4      4
002     1      2
003     2      1

So on…,

How to count the row value if it is not equal to null?

Need Query Help.

+2  A: 

that seem to work?

SELECT Table1.Field1, Count(Table1.Field2) AS CountOfField2, Count(Table1.Field3) AS CountOfField3
FROM Table1
GROUP BY Table1.Field1;

and in your example, 003 is 2 and 1, not 1 and 0

result is:

 Field1 CountOfField2 CountOfField3
 1         4                4
 2         1                2
 3         2                1

which seem to be valid if field are NULL

Fredou
@Fredou. It showing Total Count of the Row, I need only row count which is not equal to null
Gopal
for me running the query above give me, 001: 4,4 002: 1,2 003: 2,1
Fredou
Add this to the end and see what happens:WHERE isnull(Value1, 'null') <> 'null' or isnull(Value2, 'null') <> 'null'
Mr. Smith
Null values are not included in the count function.
Jeff O
A: 

I think I understand, but you may need to tweek this a bit... The Access database engine's IIF() function does a test, if TRUE, return value 1, otherwise return value 0.

select 
      EmpID, 
      sum( IIF( IsNull( Value1 ), 0, 1 )) as Fld1WithValues, 
      sum( IIF( IsNull( Value2 ), 0, 1 )) as Fld2WithValues 
   from 
      Table1 
   group by 
      EmpID;

So, even though you may have 6 entries for EmpID 1, but 2 are NULL, by doing a SUM() of either a 0 or 1, all records get counted for each field respectively and nothing is lost in the case the either/or values of Value 1 or Value 2 being null. They are tested on their OWN values.

DRapp
@DRapp - Getting only the total count of all values.
Gopal
The query will return the COUNTS.. even though it is a SUM() aggregation. If a value is NULL, it return 0, otherwise returns 1. So, for employee 1 ID you would sum: 1 + 0 + 1 + 1 + 0 + 1 = 4 under the Value1 field count, and sum: 0 + 1 + 1 + 0 + 1 + 1 = 4 for the Value2 field count.
DRapp
+1  A: 

If you want to go across the entire table

SELECT
    SUM (IIF(value1 IS NULL, 0, 1)) as NumNonNullValue1,
    SUM (IIF(value2 IS NULL, 0, 1)) as NumNonNullValue2

FROM Employee

but if you want Non Nulls per employee id then

SELECT
    EmpID,
    SUM (IIF(value1 IS NULL, 0, 1)) as NumNonNullValue1,
    SUM (IIF(value2 IS NULL, 0, 1)) as NumNonNullValue2

FROM Employee

GROUP BY EmpID

of course, sometimes just by EmpId is useful for querying, but not for display. You need the name, so we add the name field(s)

SELECT
    EmpID,
    EmployeeName,
    SUM (IIF(value1 IS NULL, 0, 1)) END as NumNonNullValue1,
    SUM (IIF(value2 IS NULL, 0, 1)) END as NumNonNullValue2

FROM Employee

GROUP BY EmpID, EmpoyeeName
Raj More
@Raj - Am getting a total count for all values. I am not getting a total count for non - null values
Gopal
Sorry, i missed out the access 2003 part. I answered with the SQL Server syntax.
Raj More
A: 

This is an Access-specific answer, but that's the context of the original question. You want to count the incidence of non-Null values, not the number of records. Several answers have used IIf() to test for Null, but you can do that much more easily with IsNull() and Abs():

  SELECT EmpID, Abs(Sum(Not IsNull([Value1]))) As Value1Count, 
     Abs(Sum(Not IsNull([Value2]))) As Value2Count
  FROM MyTable
  GROUP BY EmpID

That seems much simpler to me than using IIf().

David-W-Fenton
Simpler is in the eye of the beholder ;) IIF() is easier to transform to a CASE..WHEN..ELSE..END expression when this gets ported to a 'proper' SQL implementation.
onedaywhen
I think when you say "proper" you misspelled "different."
David-W-Fenton
'proper' = modern, industrial strength, dedicated SQL product that closely conforms to ISO SQL Standards. Is there any good reason why the Access database engine does not support the SQL-92 CASE expression syntax?
onedaywhen
Because it has never been implemented? Every SQL database engine has its own dialects. Jet/ACE SQL has a bunch of very useful features that are not available in other so-called "proper" SQL implementations (and has had many of these since SQL was introduced into Jet 2), but I don't see you criticizing the others for lacking these.
David-W-Fenton
I think every SQL product should be compliant with entry level SQL-92 as a bare minimum. If you know of any that do not then line them up and I'll criticize them ;)
onedaywhen
Jet/ACE is a legacy product. Until Access 2002, Access was using the same basic SQL dialect it had used since SQL as introduced into it, back with Access 2 or 1.1 (I don't know which -- nobody seems to document this, and I didn't use Access until version 2, and don't know what the Jet version numbers were before Jet 2.x). So, you're seeing a gradual transition to more compliant SQL. But it takes time. Now that the Access team has their own private version of the database engine, they should be able to do more of this. You might want to hit them up on that.
David-W-Fenton