Connecting Tech Pros Worldwide Forums | Help | Site Map

Count two fields based on Date in a Query

Member
 
Join Date: May 2008
Posts: 57
#1: Nov 10 '08
I'm trying to Count data in a Query Combining two different fields based on the "Date" the product was sold, and for some reason I get a wrong data. When I use only this expression, It works fine:
Code:
Count(IIf([BDC Info Table].[Sold Date]>=#11/1/2008#,0)) AS [CountNovSold]

But when I want to Count another field based on the same Date.
Code:
Count(IIf([BDC Info Table].[Status]="Sold Not Set",0) And (IIf([BDC Info Table].[Sold Date]>=#11/1/2008#,0))) AS [CountOfSold Not Set]


It gives me a wrong data. Apparently, It doesn't recognize the exact Date.
Any Help would be greatly appreciated

plaguna
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#2: Nov 11 '08

re: Count two fields based on Date in a Query


Try it this way:

Expand|Select|Wrap|Line Numbers
  1. (Count(IIf([BDC Info Table].[Status]="Sold Not Set",0)) + Count(IIf([BDC Info Table].[Sold Date]>=#11/1/2008#,0))) AS [CountOfSold Not Set]
Member
 
Join Date: May 2008
Posts: 57
#3: Nov 17 '08

re: Count two fields based on Date in a Query


Actually I want to retrieve [Status ]=“Sold not Set” when [Sold Date] is >=11/01/2008.
I found that Count will count all non-null values. That’s why I didn’t get the right data.
What do you think about this expression.
Code:

Expand|Select|Wrap|Line Numbers
  1. Abs(Sum([BDC Info Table].[Status]="Sold Not Set" And [BDC Info Table].[Sold Date]<#11/1/2008#)) AS [CountOfSold Not Set]
plaguna
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#4: Nov 18 '08

re: Count two fields based on Date in a Query


Quote:

Originally Posted by plaguna

Actually I want to retrieve [Status ]=“Sold not Set” when [Sold Date] is >=11/01/2008.
I found that Count will count all non-null values. That’s why I didn’t get the right data.
What do you think about this expression.
Code:

Expand|Select|Wrap|Line Numbers
  1. Abs(Sum([BDC Info Table].[Status]="Sold Not Set" And [BDC Info Table].[Sold Date]<#11/1/2008#)) AS [CountOfSold Not Set]
plaguna

Does your expression work for you? Also, in regards to your statement count not including null values, you should be aware that Count(*) counts null and non-null values. Count([SomeField]) will only count non-null values.

If your expression above does not work, you can try this variation of the one I gave you previously that utilizes the null to zero function, which should convert your nulls to zeros in the count and give you the answer you are looking for.

Expand|Select|Wrap|Line Numbers
  1. nz(Count(IIf([BDC Info Table].[Status]="Sold Not Set",0)),0) + nz(Count(IIf([BDC Info Table].[Sold Date]>=#11/1/2008#,0))),0) AS [CountOfSold Not Set]
Reply