5

I need to create something like this

SELECT x.id
   , x.name
   , x.type
   ,(
      IF x.type = 1
         (SELECT SUM(Col1) FROM TableA WHERE ... etc)
      ELSE IF x.type = 2
         (SELECT SUM(Col2) FROM TableB WHERE ... etc)
    ) AS Total
FROM TableX as x

So I am trying to select a different sub query according to the value of x.type

Wing

4
  • 2
    Do what? Please ask a question :) Commented Sep 26, 2016 at 11:27
  • You cannot select more than one column from sub-query' Commented Sep 26, 2016 at 11:28
  • Apologies I will edit post Commented Sep 26, 2016 at 11:29
  • 2
    If does not work INSIDE a query. You can use CASE WHEN <statement> THEN <true> ELSE <false> END instead. Commented Sep 26, 2016 at 11:59

5 Answers 5

2

Try to use LEFT JOIN and COALESCE. Use your conditions of x.type to join the tables.

COALESCE (Transact-SQL): Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL. https://msdn.microsoft.com/en-us/library/ms190349.aspx

SELECT x.id
   , x.name
   , x.type
   , COALESCE(SUM(TableA.Column), SUM(TableB.Column)) as column_xyz
FROM TableX as x
LEFT JOIN TableA ON x.type = 1 AND ...
LEFT JOIN TableB ON x.type = 2 AND ...

You can also use CASE WHEN ... THEN ... instead of COALESCE to define which column to use.

Sign up to request clarification or add additional context in comments.

Comments

1

Use CASE statement

SELECT x.id,
       x.name,
       x.type,
       CASE
         WHEN x.type = 1 THEN (SELECT Sum(Col1)
                               FROM   TableA Where...)
         WHEN x.type = 2 THEN (SELECT Sum(Col2)
                               FROM   TableB  Where .. )
       END AS Total
FROM   TableX AS x 

Comments

1

You can use CASE WHEN as the below:

SELECT 
    x.id, 
    x.name, 
    x.type,
    CASE 
        WHEN x.type = 1 THEN (SELECT SUM(A.Col1) FROM TableA A WHERE 1 = 1)
        WHEN x.type = 2 THEN (SELECT SUM(B.Col2) FROM TableB B WHERE 1 = 1)
        ELSE NULL END AS Total
FROM 
    TableX as x

Comments

1

You can use case expression:

select t.* ,
            Case when t.type = 1 then (select sum(col1) ... TableA)
                      when t.type = 2 then (select sum(col2) ... TableB)
             End as Total
From tableX t

Comments

0

By using variable -

DECLARE @SumA INT = SELECT SUM(Col1) FROM TableA WHERE ... etc
DECLARE @SumB INT = SELECT SUM(Col2) FROM TableB WHERE ... etc

SELECT x.id
   , x.name
   , x.type
   ,( CASE x.type
        WHEN 1 THEN @SumA
        WHEN 2 THEN @SumB
      END
    ) AS Total
FROM TableX as x

Choose the datatype for Sum variable accordingly (if Decimal).

2 Comments

Would the variable queries be run before the main query is run or only when they are called?
It has to be run in a single batch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.