Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:12+00:00 2026-06-13T22:55:12+00:00

There must be a better way to write this, but I’m just not sure

  • 0

There must be a better way to write this, but I’m just not sure what that is. Basically I’m trying to count the distinct values from one column where a condition is met in a separate column. I found this link, but am not sure how to apply it here.

Here is the query, I am using SQL Server 2008R2

SELECT lot,
    (SELECT COUNT(DISTINCT d.pid) FROM invdet d WHERE upk = 0 and d.lot = [invdet].lot) as noUpk,
    (SELECT COUNT(DISTINCT d.pid) FROM invdet d WHERE upk = 1 and d.lot = [invdet].lot) as isUpk
FROM invdet
WHERE ([status] in ('PQ','P2','FA','F2','BH','RL','SC','LD','PS'))
GROUP BY lot
HAVING COUNT(CASE WHEN invdet.upk = 1 THEN 1 ELSE null END) > 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:55:12+00:00Added an answer on June 13, 2026 at 10:55 pm

    You can use the fact that COUNT DISTINCT does not count NULLs to your advantage. In order to examine all the rows, create a WHERE EXISTS clause instead of limiting your result set to those statuses:

    SELECT lot,
        COUNT(DISTINCT (CASE WHEN upk = 0 THEN pid ELSE NULL END)) as noUpk
        COUNT(DISTINCT (CASE WHEN upk = 1 THEN pid ELSE NULL END)) as isUpk
    FROM invdet q
    WHERE EXISTS
    (
        SELECT 1 
        FROM invdet i 
        WHERE i.[status] in ('PQ','P2','FA','F2','BH','RL','SC','LD','PS')
        AND i.lot = q.lot
    )
    GROUP BY lot
    HAVING COUNT(CASE WHEN upk = 1 THEN 1 ELSE null END) > 0
    

    You can also effectively move the HAVING clause into a WHERE EXISTS clause as well, which may be faster, resulting in:

    SELECT lot,
        COUNT(DISTINCT (CASE WHEN upk = 0 THEN pid ELSE NULL END)) as noUpk
        COUNT(DISTINCT (CASE WHEN upk = 1 THEN pid ELSE NULL END)) as isUpk
    FROM invdet q
    WHERE EXISTS
    (
        SELECT 1 
        FROM invdet i 
        WHERE i.[status] in ('PQ','P2','FA','F2','BH','RL','SC','LD','PS')
        AND i.lot = q.lot
    )
    WHERE EXISTS
    (
        SELECT 1 
        FROM invdet i 
        WHERE i.upk = 1 
        AND i.lot = q.lot
    )
    GROUP BY lot
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sure there must be a relatively straightforward way to do this, but
It feels like there must be some semi-simple solution to this, but I just
Is there any better and optimized way to write this code? public int ValidateEntries()
I'm guessing that there must be a better functional way of expressing the following:
There must be a simple solution to this problem but for the love of
There must be an easier way or function to do this code here: #!/usr/bin/env
EDIT: There must be some way I can approach this without writing a whole
I'm pretty sure there must be a way to launch spotify iphone app from
I know there must be a really simple answer to this question, but I
There must be something wrong. I don't know what? see this on jsfiddle .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.