2017-03-27 66 views
0

我需要一些帮助。TSQL - 将两个虚拟表与左外部连接相结合

我有一个名为@combine

下面表格是它的结果

enter image description here

我有表@monthofyear和绘制表格我想(见图片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 

它不给我绘制表格布局。有人可以帮帮我吗?

电流输出:

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代码****

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 

代码在这里

+3

以文本形式发布表格和数据[阅读此](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when - 问题/ 285557#285557) –

+0

你的意思是'不给我DRAW图表布局.'?问题是什么? –

+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) 
+0

我试过了上面的代码,得到的结果与我在“当前输出”中发布的结果相同 – user396123

+0

首先检查“交叉连接”以确保所有组合都是'(月份,类型)'。同时删除最后一个'GROUP BY' –

+0

顺便说一句,你不需要'YEAR'来区分'2017-01'和'2016-01'? –

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表后,我们已筛选结果。你可能需要做一些事情的NULL,你将在AllAmount和AmountType列得(如ISNULL(AllAmount,0)AS AllAmount)

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

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

+0

我它有点工作。我增加了额外的几个月,并设法将AMOUNT字段设置为0.但是,如何填充phil字段?它也显示为null加入左连接的月份。 – user396123

+0

我在问题的最后发布了OUTPUT顶部 – user396123

+0

我已经更新了答案。 –