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 688883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:16:31+00:00 2026-05-14T02:16:31+00:00

I am trying to add a CASE statement to the end of my SQL

  • 0

I am trying to add a CASE statement to the end of my SQL query to compute a value depending upon another table value and a previously computed value in the SELECT. The error is returned that DelivCount is an invalid column name. Is there a better way to do this or am I missign something?

SELECT jd.FullJobNumber, jd.ProjectTitle, jd.ClientName, jd.JobManager, jd.ProjectDirector, jd.ServiceGroup, jd.Status, jd.HasDeliverables, jd.SchedOutsideJFlo, jd.ReqCompleteDate,(SELECT COUNT(*)FROM DeliverablesSchedule ds WHERE jd.FullJobNumber = ds.FullJobNumber) as DelivCount, SchedType = 
    CASE 
        WHEN (jd.SchedOutsideJFlo = 'Yes')
            THEN 'outside'
        WHEN (jd.HasDeliverables = 'No ')
            THEN 'none'
        WHEN (DelivCount > 0)
            THEN 'has'
        WHEN (jd.HasDeliverables = 'Yes' AND DelivCount = 0)
            THEN 'missing'
        ELSE 'unknown'
    END
FROM JobDetail jd
  • 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-05-14T02:16:31+00:00Added an answer on May 14, 2026 at 2:16 am

    try this, which should run a lot faster:

    SELECT 
        jd.FullJobNumber, jd.ProjectTitle, jd.ClientName, jd.JobManager, jd.ProjectDirector, jd.ServiceGroup, jd.Status, jd.HasDeliverables, jd.SchedOutsideJFlo, jd.ReqCompleteDate
            ,ds.DelivCount
            ,SchedType =CASE 
                            WHEN (jd.SchedOutsideJFlo = 'Yes')
                                THEN 'outside'
                            WHEN (jd.HasDeliverables = 'No ')
                                THEN 'none'
                            WHEN (ds.DelivCount > 0)
                                THEN 'has'
                            WHEN (jd.HasDeliverables = 'Yes' AND ds.DelivCount = 0)
                                THEN 'missing'
                            ELSE 'unknown'
                        END
        FROM JobDetail jd
            LEFT OUTER JOIN (SELECT
                                 FullJobNumber, COUNT(*) AS DelivCount
                                 FROM DeliverablesSchedule
                                 GROUP BY FullJobNumber
                            ) ds ON jd.FullJobNumber = ds.FullJobNumber
    

    The original query uses a subquery:

    A subquery is a SELECT query that
    returns a single value and is nested
    inside a SELECT, INSERT, UPDATE, or
    DELETE statement, or inside another
    subquery. A subquery can be used
    anywhere an expression is allowed.

    by the very nature of a sub query, it must be run repeatedly, once for each row. I have rewritten the query to use a derived table, which is evaluated one time to find all of the counts and is then joined to the proper rows. This allows for the DelivCount value to be referred to as any column would be when joined from another table, and should speed up this query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a query in my current SQL INSERT INTO statement.
I'm trying to add a value (in this case the string I Added This)
I'm trying to add a case or if statement in the where clause of
I am trying to add a key from one table to another so that
I'm trying to conditionally join one master event table to three others depending on
I am trying to create a query in MS SQL 2005 that will return
I'm trying to convert a PostgreSQL into SQL Server. But this query doesn't work.
I'm trying to dynamically add class properties into an PDO prepared statement. To achieve
I am trying to create an SQL select statement in PL/pgSQL procedure. It was
I am trying to add NOT NULL and a DEFAULT to an existing table.

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.