Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2013
    Posts
    3

    Unanswered: Distinct Count Error

    I need to write a select statement I am unsure how to do this correctly. My psuedo code would be,
    IF first_gift_date <> NULL THEN COUNT(item_id). However, the item_id must be distinct.

    I tried the following:
    SELECT
    COUNT(CASE WHEN first_gift_date <> NULL THEN (DISTINCT item_id) end)
    FROM table_a

    adding DISTINCT does not work and I am unsure how to get a distinct count using a case expression.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Keep in mind that this is untested "air code", but I'd try:
    Code:
    SELECT Count(DISTINCT CASE WHEN first_gift_date IS NOT NULL THEN item_id END)
       FROM table_a
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Mar 2013
    Posts
    3

    Distinct Count Error

    THANK YOU!!!! That worked like a charm!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •