Count two fields based on Date in a Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plaguna
    New Member
    • May 2008
    • 58

    Count two fields based on Date in a Query

    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
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Try it this way:

    Code:
    (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]

    Comment

    • plaguna
      New Member
      • May 2008
      • 58

      #3
      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:

      Code:
      Abs(Sum([BDC Info Table].[Status]="Sold Not Set" And [BDC Info Table].[Sold Date]<#11/1/2008#)) AS [CountOfSold Not Set]
      plaguna

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        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:

        Code:
        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.

        Code:
        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]

        Comment

        Working...