Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the result of a subquery in a CASE expression with T-SQL

I'm writing a query with some CASE expressions and it outputs helper-data columns which help me determine whether or not a specific action is required. I would like to know if I can somehow use the result of a subquery as the output without having to perform the same query twice (between WHEN (subquery) THEN and as the result after THEN)

The dummy code below describes what I'm after. Can this be done? I'm querying a MS2005 SQL database.

SELECT   'Hello StackOverflow'
        ,'Thanks for reading this question'
        ,CASE
            WHEN 
                (
                    SELECT count(*)
                    FROM    sometable
                    WHERE   condition = 1
                    AND     somethingelse = 'value'
                ) > 0 THEN
                    -- run the query again to get the number of rows
                    (
                        SELECT count(*)
                        FROM    sometable
                        WHERE   condition = 1
                        AND     somethingelse = 'value'
                    )
            ELSE 0
         END

SELECT   'Hello StackOverflow'
        ,'Thanks for reading this question'
        ,CASE
            WHEN 
                (
                    SELECT count(*)
                    FROM    sometable
                    WHERE   condition = 1
                    AND     somethingelse = 'value'
                ) AS subqry_count > 0 THEN
                    -- use the subqry_count, which fails... "Incorrect syntax near the keyword 'AS'"
                    subqry_count
            ELSE 0
         END
like image 880
Ben Fransen Avatar asked Jun 24 '15 13:06

Ben Fransen


People also ask

Can we use subquery IN CASE statement in SQL?

A subquery in the ELSE clause works the same way as a subquery in the THEN clause. We use a passthru predicate to evaluate the subquery conditionally. Similarly, a CASE expression with multiple WHEN clauses with subqueries in each THEN clause also works the same way.

Can I use SELECT query in case statement?

The case statement in SQL returns a value on a specified condition. We can use a Case statement in select queries along with Where, Order By, and Group By clause. It can be used in the Insert statement as well.

Can CTE be used in subquery?

¶ A CTE (common table expression) is a named subquery defined in a WITH clause. You can think of the CTE as a temporary view for use in the statement that defines the CTE. The CTE defines the temporary view's name, an optional list of column names, and a query expression (i.e. a SELECT statement).

What will be the output of a case statement when all condition checks are true?

Once a condition is found to be true, the CASE statement will return the result and not evaluate the conditions any further. All conditions must be the same datatype. The value returned once a condition is found to be true. All values must be the same datatype.


1 Answers

Just use the subquery as the source you are selecting from:

SELECT   'Hello StackOverflow'
        ,'Thanks for reading this question'
        ,CASE subqry_count.Cnt
          WHEN 0 THEN 0
          ELSE subqry_count.Cnt
        END
  FROM ( SELECT count(*) AS Cnt
                    FROM    sometable
                    WHERE   condition = 1
                    AND     somethingelse = 'value'
                )  subqry_count

As an aside, if you are just going to return 0 if the output from COUNT is 0, then you don't even need to use a CASE statement.

like image 68
Martin Avatar answered Oct 12 '22 13:10

Martin