TSQL - 将两个虚拟表与左外连接组合在一起

时间:2017-03-27 15:05:04

标签: sql sql-server reporting-services outer-join

我需要一些帮助。

我有一个名为@combine

的表

以下是结果

enter image description here

我有@monthofyear表和我想要的DRAW表(见图2) enter image description here

我想做一个查询,它会给我这个DRAW表,所以我可以在SSRS中绘制一个图表

我在很多论坛上说我必须在表之间进行LEFT OUTER JOIN才能激活DRAW表。

这是我的代码:

select ss.Phil
    ,ss.amounttype
    ,m.months
    ,ss.allmount
from @monthofyear as m
left outer join (
    select c.phil
        ,c.amounttype
        ,c.month_name
        ,sum(COALESCE(c.amount, 0)) as allmount
    from @combine as c
    group by phil
        ,amounttype
        ,month_name
    ) ss on (m.months = ss.month_name)
where ss.phil = 'F-14-0023'
group by ss.phil
    ,ss.amounttype
    ,m.months
    ,ss.allmount

它没有给我DRAW表格布局。有人可以帮助我吗?

CURRENT OUTPUT:

Phil    amounttype  months  allmount
F-14-0023   ACTUAL  February    594.46
F-14-0023   ACTUAL  January 7019.23
F-14-0023   BUDGET  April   1340.00
F-14-0023   BUDGET  December    282500.00
F-14-0023   BUDGET  February    1340.00
F-14-0023   BUDGET  January 1340.00
F-14-0023   BUDGET  July    -282647.00
F-14-0023   BUDGET  March   1340.00
F-14-0023   FORECAST    March   1303.36

期望的输出

Phil    amounttype  months  allmount
F-14-23 ACTUAL  Feb 594.46
F-14-23 ACTUAL  Jan 7019.23
F-14-23 ACTUAL  Mar 0
F-14-23 ACTUAL  Apr 0
F-14-23 ACTUAL  May 0
F-14-23 ACTUAL  June    0
F-14-23 ACTUAL  July    0
        (until December)    
F-14-23 BUDGET  Apr 1340
F-14-23 BUDGET  Dec 282500
F-14-23 BUDGET  Feb 1340
F-14-23 BUDGET  Jan 1340
F-14-23 BUDGET  July    -282647
F-14-23 BUDGET  Mar 1340
F-14-23 BUDGET  Jan 0
F-14-23 BUDGET  May 0
F-14-23 BUDGET  June    0
        (all other months)  
F-14-23 FORECAST    Mar 130.36
        (all other months)  

**使用ALAN&#39的代码输出****

 Phil   amounttype  monthsname  AllAmount
 Phil   amounttype  monthsname  AllAmount
  NULL  NULL    August  0
  NULL  NULL    June    0
  NULL  NULL    May 0
  NULL  NULL    November    0
  NULL  NULL    October 0
  NULL  NULL    September   0
  F-14-0023 BUDGET  April   1340
  F-14-0023 BUDGET  December    282500
  F-14-0023 BUDGET  February    1340
  F-14-0023 BUDGET  January 1340
  F-14-0023 BUDGET  July    -282647
  F-14-0023 BUDGET  March   1340

代码

2 个答案:

答案 0 :(得分:0)

现在您有month的表格,但每个月还需要type

SELECT ss.Phil,
       t.amounttype,
       m.months,
       ss.allmount
FROM @monthofyear as m
CROSS JOIN (SELECT DISTINCT amounttype  -- CREATE ALL (month, type)
            FROM @combine) as t
LEFT JOIN (
    select 'F-14-0023' as phil,
           c.amounttype,
           c.month_name,
           sum(COALESCE(c.amount, 0)) as allmount
    from @combine as c
    WHERE ss.phil = 'F-14-0023'
    group by phil,
             amounttype,
             month_name
    ) ss 
   ON m.months = ss.month_name
  AND t.amounttype = ss.amounttype  -- Match with the above (month, type)

答案 1 :(得分:0)

问题在于您要加入两个表格,然后将结果过滤到ss.phil =' F-14-0023'。您需要在内部查询中移动WHERE子句。根据您的原始尝试,它将如下所示。

DECLARE @phil varchar(30)=' F-14-0023'

select @phil
    , CASE WHEN ss.amounttype NOT NULL THEN ss.AmountType
      ELSE 
         CASE WHEN month_number <= month(getdate()) THEN 'ACTUAL'
         ELSE 'BUDGET'
         END
      END AS AmountType
    ,m.months
    ,ss.allmount
from @monthofyear as m
left outer join (
    select c.phil
        ,c.amounttype
        ,c.month_name
        ,sum(COALESCE(c.amount, 0)) as allmount
    from @combine as c
    WHERE c.Phil = 'F-14-0023'
    group by phil
        ,amounttype
        ,month_name
    ) ss on (m.months = ss.month_name)
group by ss.phil
    ,ss.amounttype
    ,m.months
    ,ss.allmount

现在我们正在做的是拿着你的月份表,在我们筛选结果后,将它加入你的@Combine表。您可能需要对AllAmount和AmountType列中的NULLS做一些事情(例如ISNULL(AllAmount,0)AS AllAmount)

更新假设&#39; F-14-0023&#39;作为参数传入,只需使用它代替select中的ss.Phil字段(在第一行) amounttype列woudl必须是基于月份的CASE语句。您可能需要将month_number colume添加到@monthofyear表。如果它应根据您的要求读取预测,您可以将其扩展为clculate。我已经更新了上面的查询来说明。

代码中可能存在错误,因为我没有时间设置测试数据,但它应该足够接近。