Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Unanswered: Perform Calculation Based Off case Statement?

    Would it be possible for me to do what I am attempting below? Obviously it isn't working for me, haha
    Code:
    create table #Try
    (
    ActiveItemsToSell int,
    TotalItemsToSell int,
    ItemsSoldOut int
    )
    
    insert into #Try
    (
    ActiveItemsToSell,
    TotalItemsToSell,
    ItemsSoldOut
    )
    Select
    count(distinct case when soldOut is null then ID else null end),
    count(distinct id),
    --I want to do this, but it errors each time
    COUNT(Distinct id) - count(distinct case when soldOut is null then ID else null end)
    Last edited by jo15765; 11-12-13 at 16:26. Reason: wrng code tags

  2. #2
    Join Date
    Aug 2012
    Posts
    41
    I tried your code and it works, what error do you get ?

  3. #3
    Join Date
    Feb 2012
    Posts
    188
    Quote Originally Posted by alexandra123 View Post
    I tried your code and it works, what error do you get ?
    Left out the error message -- I am def not on my "A" game today. I get th error of Divide by zero error encountered.

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    I don't see any division going on. Should the '-' be a '/'?

  5. #5
    Join Date
    Feb 2012
    Posts
    188
    Quote Originally Posted by MCrowley View Post
    I don't see any division going on. Should the '-' be a '/'?
    Grrr.. Yes. Thousand apologies.

  6. #6
    Join Date
    Aug 2012
    Posts
    41
    also should this count(distinct case when soldOut is null then ID else null end

    not be count(distinct case when soldOut is null then ID else 0 end)

  7. #7
    Join Date
    Feb 2012
    Posts
    188
    Quote Originally Posted by alexandra123 View Post
    also should this count(distinct case when soldOut is null then ID else null end

    not be count(distinct case when soldOut is null then ID else 0 end)
    It executes leaving it as null, but makes more sense to change it to a 0.
    And changing to a 0 removes the error.
    Sorry for the nuisance today!

Posting Permissions

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