How to merge sql query with union into one query

Seeker

I would like to get the results of the below query with just one query not by using union. My query is as below

I am generating a SSRS chart this query, so need to merge the query into one and get a proper result as shown in table 2

select 
    res.Count, res.Month, res.status, res.SortOrder 
from 
    (SELECT 
        count(analysis_complete_date) as Count,
        DATENAME(month, analysis_complete_date) AS Month, 
        DATEPART(month, analysis_complete_date) AS SortOrder,
        'Analysis' as status
     FROM  
        SCN_Part_Details AS parts
     WHERE  
        analysis_complete_date BETWEEN '2014-01-01' AND '2014-12-11'
     GROUP BY 
        DATENAME(month, analysis_complete_date), 
        DATEPART(month, analysis_complete_date)

     union

     SELECT 
        count(Act_Supp_Negotiation_Date) as Count,
        DATENAME(month, Act_Supp_Negotiation_Date) AS Month, 
        DATEPART(month, Act_Supp_Negotiation_Date) AS SortOrder,
        'Negotiated' as status
     FROM  
        SCN_Part_Details AS parts
     WHERE  
        Act_Supp_Negotiation_Date BETWEEN '2014-01-01' AND '2014-12-11'
     GROUP BY 
        DATENAME(month, Act_Supp_Negotiation_Date), 
        DATEPART(month, Act_Supp_Negotiation_Date) ) as res
order by 
    res.SortOrder

This will give a result like:

Table 1

Count          Month          Status     SortOrder
--------------------------------------------------
167            January       Analysis     1
631            January       Negotiated   1
70             February      Analysis     2
237            February      Negotiated   2

and so on..

I want a result like this:

Table 2

AnalysisCount    NegotiatedCount    Month       SortOrder
---------------------------------------------------------
167                 631             January      1
70                  237             February     2
Veera

Give it a try:

 ;WITH CTEResult AS 
(
    select 
        res.Count, res.Month, res.status, res.SortOrder 
    from 
        (SELECT 
            count(analysis_complete_date) as Count,
            DATENAME(month, analysis_complete_date) AS Month, 
            DATEPART(month, analysis_complete_date) AS SortOrder,
            'Analysis' as status
         FROM  
            SCN_Part_Details AS parts
         WHERE  
            analysis_complete_date BETWEEN '2014-01-01' AND '2014-12-11'
         GROUP BY 
            DATENAME(month, analysis_complete_date), 
            DATEPART(month, analysis_complete_date)

         union

         SELECT 
            count(Act_Supp_Negotiation_Date) as Count,
            DATENAME(month, Act_Supp_Negotiation_Date) AS Month, 
            DATEPART(month, Act_Supp_Negotiation_Date) AS SortOrder,
            'Negotiated' as status
         FROM  
            SCN_Part_Details AS parts
         WHERE  
            Act_Supp_Negotiation_Date BETWEEN '2014-01-01' AND '2014-12-11'
         GROUP BY 
            DATENAME(month, Act_Supp_Negotiation_Date), 
            DATEPART(month, Act_Supp_Negotiation_Date) ) as res
)

SELECT DISTINCT
  (SELECT TOP 1 Count FROM CTEResult A WHERE A.Month = C.Month AND A.STATUS =  'Analysis' AND A.SortOrder = C.SortOrder) AS  AnalysisCount,
  (SELECT TOP 1 Count FROM CTEResult B WHERE B.Month = C.Month AND B.STATUS =  'Negotiated' AND B.SortOrder = C.SortOrder) AS  
    NegotiatedCount, C.Month, C.SortOrder 
FROM CTEResult C

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Iterative union SQL query

分類Dev

SQL query two queries and union

分類Dev

SQL 2012 Simplify Query Union

分類Dev

Merge Three Tables and get one output using SQL query

分類Dev

SQL Server - Merge multiple query results into one result set

分類Dev

Collation conflict in SQL Union All Query

分類Dev

SQL - How to GROUP output of one query using sql?

分類Dev

updating totals with one sql query

分類Dev

SQL query to find How many employees joined in one week

分類Dev

SQL query to find How many employees joined in one week

分類Dev

How to use the results of one query in the next query?

分類Dev

how to reconstruct this sql query

分類Dev

Pivoting a union query

分類Dev

Union query with literals in InterBase

分類Dev

how to change this sql query to hibernate query?

分類Dev

SQL Server query with intersect except or union relational Algebra

分類Dev

One query or quick way to execute sql statement

分類Dev

How to simplify following SQL query?

分類Dev

How to concatenate sql query in java?

分類Dev

How to tune an Oracle SQL query

分類Dev

How to use NOT in relational SQL query

分類Dev

Query to merge (some) rows

分類Dev

PHP SQL Join Query merge content in Multi-Array

分類Dev

SQL Query to merge two tables with different timestamp as index

分類Dev

SQL Server How to output one table result from multiple results with a WHILE query

分類Dev

How to fetch all the batches for a query at one go using stored proc in azure SQL db?

分類Dev

How does one write an SQL query to add up a total of balances for each user's most recent transaction?

分類Dev

How can I make this SQL query work to prevent the "subquery returned more than one row" error?

分類Dev

Sql query with Joins into Linq query

Related 関連記事

  1. 1

    Iterative union SQL query

  2. 2

    SQL query two queries and union

  3. 3

    SQL 2012 Simplify Query Union

  4. 4

    Merge Three Tables and get one output using SQL query

  5. 5

    SQL Server - Merge multiple query results into one result set

  6. 6

    Collation conflict in SQL Union All Query

  7. 7

    SQL - How to GROUP output of one query using sql?

  8. 8

    updating totals with one sql query

  9. 9

    SQL query to find How many employees joined in one week

  10. 10

    SQL query to find How many employees joined in one week

  11. 11

    How to use the results of one query in the next query?

  12. 12

    how to reconstruct this sql query

  13. 13

    Pivoting a union query

  14. 14

    Union query with literals in InterBase

  15. 15

    how to change this sql query to hibernate query?

  16. 16

    SQL Server query with intersect except or union relational Algebra

  17. 17

    One query or quick way to execute sql statement

  18. 18

    How to simplify following SQL query?

  19. 19

    How to concatenate sql query in java?

  20. 20

    How to tune an Oracle SQL query

  21. 21

    How to use NOT in relational SQL query

  22. 22

    Query to merge (some) rows

  23. 23

    PHP SQL Join Query merge content in Multi-Array

  24. 24

    SQL Query to merge two tables with different timestamp as index

  25. 25

    SQL Server How to output one table result from multiple results with a WHILE query

  26. 26

    How to fetch all the batches for a query at one go using stored proc in azure SQL db?

  27. 27

    How does one write an SQL query to add up a total of balances for each user's most recent transaction?

  28. 28

    How can I make this SQL query work to prevent the "subquery returned more than one row" error?

  29. 29

    Sql query with Joins into Linq query

ホットタグ

アーカイブ