如何将sql查询与union合并到一个查询中

时间:2014-12-12 10:04:02

标签: sql sql-server sql-server-2008

我想通过一个查询而不是使用union来获得以下查询的结果。 我的查询如下

我正在为此查询生成SSRS图表,因此需要将查询合并为一个并获得正确的结果,如表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

这将产生如下结果:

表1

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

依旧......

我想要一个这样的结果:

表2

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

2 个答案:

答案 0 :(得分:0)

试一试:

 ;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

答案 1 :(得分:0)

获得结果后,您可以转动表格以获得所需的结果。

create table #temp
(
    quantity int,
    month varchar(20),
    status varchar(20),
    sortorder int
)

insert into #temp values                                
(167,'January' ,'Analysis', 1    ),
(631,'January' ,'Negotiated',1   ),
(70 ,'February','Analysis', 2    ),
(237,'February','Negotiated', 2  )


select max(analysis) as analysiscount    
      ,max(negotiated) as negotiatedcount    
      ,month
      ,max(sortorder) as sortorder    
from 
    (
        select * 
        from #temp
        pivot
            (max(quantity) for status in ([analysis],[negotiated])) as pi
    ) t
group by month
order by sortorder;