1

I am having troubles combining multiple queries into one output. I am a beginner at SQL and was wondering if anyone can provide me with some feedback on how to get this done.

Here is my code:

SELECT [status], [queryno_i] as 'Query ID', [assigned_to_group] as 'Assigned To Group', [issued_date] as 'Issuing Date',
CASE
    WHEN [status] = 3 THEN [mutation]
    ELSE NULL
END AS 'Closing Date'
 FROM tablename.[tech_query] WITH (NOLOCK)


SELECT
CASE
    WHEN [status] = 3 THEN 'CLOSED'
    ELSE 'OPEN'
END AS [State]
 FROM tablename.[tech_query] WITH (NOLOCK)


SELECT
CASE
    WHEN [status] = 3 THEN [mutation_int]-[issued_date_INT]
    ELSE NULL
END AS [TAT]
 FROM tablename.[tech_query] WITH (NOLOCK)
7
  • 1
    You need UNION: techonthenet.com/sql/union.php Commented Dec 6, 2013 at 21:24
  • do you get any errors? Commented Dec 6, 2013 at 21:24
  • @Mörre can we use union even if there are uneven columns? Commented Dec 6, 2013 at 21:25
  • The word combine can mean so many different things Commented Dec 6, 2013 at 21:25
  • @Mörre but that was for sqllite Commented Dec 6, 2013 at 21:29

5 Answers 5

2

If you want all in one row just put all together

SELECT [status], 
      [queryno_i] as 'Query ID', 
      [assigned_to_group] as 'Assigned To Group', 
      [issued_date] as 'Issuing Date',
      CASE WHEN [status] = 3 THEN [mutation] ELSE NULL END AS 'Closing Date',
      CASE WHEN [status] = 3 THEN 'CLOSED' ELSE 'OPEN' END AS [State],
      CASE WHEN [status] = 3 THEN [mutation_int]-[issued_date_INT] ELSE NULL 
            END AS [TAT]
 FROM tablename.[tech_query] WITH (NOLOCK)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a UNION clause

http://www.w3schools.com/sql/sql_union.asp

SELECT 1 
  UNION ALL
SELECT 2
  UNION ALL
SELECT 3

However, from looking at your 3 statements, all 3 selects are coming from the same table, so you may be able to just combine and use CASE statements to get your results without using a Union.

Comments

1

With SQL, everything would have to be one result set. In order for you to return the results of all of these queries, they all would have to have the same columns (and data types).

Would you able to combine everything into one select query?

SELECT 
    [status]
    , [queryno_i] as 'Query ID'
    , [assigned_to_group] as 'Assigned To Group'
    , [issued_date] as 'Issuing Date'
    , CASE
        WHEN [status] = 3 THEN [mutation]
        ELSE NULL
        END AS 'Closing Date'
    , NULL AS State
    , NULL AS Tat
    , 1 AS QueryNumber
FROM 
    [tech_query] 

UNION ALL

SELECT NULL,NULL,NULL,NULL,NULL,
    CASE
        WHEN [status] = 3 THEN 'CLOSED'
        ELSE 'OPEN'
    END AS [State]
    , NULL
    , 2 AS QueryNumber
FROM 
    [tech_query] 

UNION ALL

SELECT NULL,NULL,NULL,NULL,NULL, NULL
CASE
    WHEN [status] = 3 THEN [mutation_int]-[issued_date_INT]
    ELSE NULL
END AS [TAT]
, 3 AS QueryNumber
FROM 
    [tech_query] 

Comments

0

Are you looking for something like this?

SELECT [status], [queryno_i] as 'Query ID', 
   [assigned_to_group] as 'Assigned To Group', [issued_date] as 'Issuing Date',
CASE
   WHEN [status] = 3 THEN [mutation]
   ELSE NULL
END AS 'Closing Date',

CASE
   WHEN [status] = 3 THEN 'CLOSED'
   ELSE 'OPEN'
END AS [State],

CASE
   WHEN [status] = 3 THEN [mutation_int]-[issued_date_INT]
   ELSE NULL
END AS [TAT]


FROM tablename.[tech_query] WITH (NOLOCK)

Comments

0

Case statements are inline

SELECT 
  [status], 
  [queryno_i] as 'Query ID', 
  [assigned_to_group] as 'Assigned To Group', 
  [issued_date] as 'Issuing Date',
  CASE
      WHEN [status] = 3 THEN [mutation]
      ELSE NULL
  END AS 'Closing Date',
  CASE
    WHEN [status] = 3 THEN 'CLOSED'
    ELSE 'OPEN'
  END AS [State],
  SELECT
  CASE
      WHEN [status] = 3 THEN [mutation_int]-[issued_date_INT]
      ELSE NULL
  END AS [TAT]
  FROM tablename.[tech_query] WITH (NOLOCK)

Why the nolock?

Comments

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.