SSRS 2008 R2需要显示一个表中的所有行,即使是null - join?

时间:2014-03-24 20:00:20

标签: reporting-services

希望有人可以提供帮助:)

我正在制作一份报告,显示当年的销售额和上一年度的销售额。我们有5个不同的产品组(建筑,植物,零售,种子和批发)和每组下的许多产品线(例如正在建设中的“设备”,植物下的“草药”等)。

我无法找到构建查询的方法,以便我可以让组和产品线并排进行匹配。今年销售额可能已售出一些集团/产品线,因为他之前没有卖出任何$,反之亦然,他可能在上一年的集团/产品线上有销售,但目前没有。所以我得到的行不匹配,因为其中一个或哪个没有记录。我想显示该组/产品系列,但销售额为0美元,因此行排成一行。基本上,我得到特定日期范围的订单标题,然后左外连接订单详细信息(实际上包含订单所针对的产品组和产品系列,而不是标题级别)和左外连接产品线desc on订单行级别的产品系列ID。

我已经尝试过左外连接,右边也是完整的,但我只收到记录,其中至少有一个该产品系列的订单。以下是我的查询;订单是订单表头,Order_Line是订单行表,Customer是客户表,Major_Grp是产品组表,Category是产品线表,Salesman是销售员表:

SELECT  ol.PRICE_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  [ORDER]  o
left outer join [order_line] ol on o.order_num = ol.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
left outer join category ca on ol.pline_Num = ca.pline_Id
left outer join major_grp m on ol.major_grp = m.major_grp
WHERE c.SLSM2_NUM IN (@Salesrep) and 
o.cust_num IN (@CustomerList) and
o.ord_date between (@ReqDateFrom2) and (@ReqDateTo2) 

如果我对该组/产品系列有任何销售,我该如何才能显示所有类别产品系列(以及所有Major_Grp产品组)? enter image description here

非常感谢您提供任何帮助! :)

ETA展示我要做的事情的一个例子 - 看看现在有什么行为施工和产品线设备在右边(先前)表,但没有销售,并进一步沿着产品线展示和排队,即使没有销售。我需要知道如何让每个组的每个产品系列都有一行,即使没有销售。看看每一行是如何产生相同的产品线? enter image description here

这里真的是我的查询的样子(编辑它以便更容易理解):

SELECT  ol.PRICE_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  [ORDER]  o
left outer join [order_line] ol on o.order_num = ol.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
right outer join category ca on ol.pline_Num = ca.pline_Id
left outer join major_grp m on ol.major_grp = m.major_grp
WHERE c.SLSM2_NUM IN (@Salesrep) and 
o.cust_num IN (@CustomerList) and
c.comm_flg IN (@CommFlg) and
o.ord_date between (@ReqDateFrom2) and (@ReqDateTo2) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
(c.slsm_num IN  (@Salesrep) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN (@Salesrep)     
and ol.major_grp IN ('P', 'S'))  and
ol.pline_num <> '905' and
ol.pline_num <> '999'
UNION ALL
SELECT  ol.NET_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  [ORDER_HISTORY]  o
left outer join order_History_line ol on o.order_num = ol.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
right outer join category ca on ol.pline_Num = ca.pline_Id
left outer join major_grp m on ol.major_grp = m.major_grp
WHERE c.SLSM2_NUM IN (@Salesrep) and 
o.cust_num IN (@CustomerList) and
c.comm_flg IN (@CommFlg) and
o.ord_date between (@ReqDateFrom2) and (@ReqDateTo2) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
(c.slsm_num IN  (@Salesrep) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN  (@Salesrep)     
and ol.major_grp IN ('P', 'S'))  and
ol.pline_num <> '905' and
ol.pline_num <> '999'
好吧,我也试过这个 - 在我的查询中创建我的第一个表类别表(包含所有产品系列)然后加入其他表格(下面的例子使用“完全加入”,我尝试过每次加入都没有运气 - 我也试过“ca.pline_Id = ol.pline_num”以及ol.pline_Num = ca.pline_Id“):

SELECT  ol.PRICE_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  CATEGORY ca
full join major_grp m on ca.major_grp = m.major_grp
full join [order_line] ol on ca.pline_Id = ol.pline_num 
full join [ORDER]  o on ol.order_num = o.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
WHERE c.SLSM2_NUM IN (@Salesrep) and 
o.cust_num IN (@CustomerList) and
c.comm_flg IN (@CommFlg) and
o.ord_date between (@ReqDateFrom) and (@ReqDateTo) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
(c.slsm_num IN  (@Salesrep) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN  (@Salesrep)      
and ol.major_grp IN ('P', 'S'))  and
ol.pline_num <> '905' and
ol.pline_num <> '999'
UNION ALL
SELECT  ol.NET_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  CATEGORY ca
full join major_grp m on ca.major_grp = m.major_grp
full join [order_history_line] ol on ca.pline_Id = ol.pline_num 
full join [ORDER_HISTORY]  o on ol.order_num = o.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
WHERE c.SLSM2_NUM IN (@Salesrep) and 
o.cust_num IN (@CustomerList) and
c.comm_flg IN (@CommFlg) and
o.ord_date between (@ReqDateFrom) and (@ReqDateTo) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
(c.slsm_num IN  (@Salesrep) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN  (@Salesrep)     
and ol.major_grp IN ('P', 'S'))  and
ol.pline_num <> '905' and
ol.pline_num <> '999'
好的,这是我到目前为止所做的工作。但是,正如我所说的,我在声明的UNION ALL部分出错了(我试图在下面将它BOLD,因此它周围的“**” - 这不是我的代码!)。有什么帮助吗?

select * from
(select ca.pline_ID, ca.pline_desc, m.major_grp_desc from CATEGORY ca
full join major_grp m on ca.major_grp = m.major_grp) t1
left outer join
(select ol.pline_num, SUM(ol.PRICE_EXT) as SumPriceExt from
[order_line] ol 
full join [ORDER] o on ol.order_num = o.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
where (c.SLSM2_NUM IN (@Salesrep)) and
(c.comm_flg IN (@CommFlg)) and
(o.ord_date between @ReqDateFrom and @ReqDateTo) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and (c.slsm_num IN (@Salesrep) and      
ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN (@Salesrep) and ol.major_grp IN ('P', 'S'))      
and
ol.pline_num <> '905' and
ol.pline_num <> '999'
group by ol.pline_num
) t2 ON t1.pline_ID = t2.pline_num order by t1.MAJOR_GRP_DESC, t1.pline_desc
**UNION ALL**
select * from
(select ca.pline_ID, ca.pline_desc, m.major_grp_desc from CATEGORY ca
full join major_grp m on ca.major_grp = m.major_grp) t1
left outer join
(select ol.pline_num, SUM(ol.Net_EXT) as SumPriceExt from
[order_history_line] ol 
full join [ORDER_history] o on ol.order_num = o.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
where (c.SLSM2_NUM IN (@Salesrep)) and
(c.comm_flg IN (@CommFlg)) and
(o.ord_date between @ReqDateFrom and @ReqDateTo) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and (c.slsm_num IN (@Salesrep) and      
ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num IN (@Salesrep) and ol.major_grp IN ('P', 'S'))      
and
ol.pline_num <> '905' and
ol.pline_num <> '999'
group by ol.pline_num
) t2 ON t1.pline_ID = t2.pline_num order by t1.MAJOR_GRP_DESC, t1.pline_desc

2 个答案:

答案 0 :(得分:0)

尝试将过滤条件作为连接条件移动。 WHERE过滤最终结果集。这可能会过滤掉记录。请尝试以下查询: -

SELECT  ol.PRICE_EXT, c.slsm2_num, s.slsm_desc, ca.pline_desc, m.major_grp_desc
FROM  [ORDER]  o
left outer join [order_line] ol on o.order_num = ol.order_num and o.cust_num IN (@CustomerList) and o.ord_date between (@ReqDateFrom2) and (@ReqDateTo2)
left outer join customer c on o.cust_num = c.cust_no and c.SLSM2_NUM IN (@Salesrep)
full outer join salesman s on c.slsm2_num = s.slsm_num
full outer join category ca on ol.pline_Num = ca.pline_Id
full outer join major_grp m on ol.major_grp = m.major_grp

答案 1 :(得分:0)

有没有办法加入&#34; Group&#34;直接到&#34; ProductLine&#34;?在片刻,您可以通过订单行表加入他们。如果订单行表是空的,那么目前没有关于哪个组&#34;灯泡&#34;属于。但是你想要的报告显示了直接关系,&#34;灯泡&#34;在小组&#34;植物&#34;,我想从名称&#34;灯泡&#34;总是在小组&#34;植物&#34;,并且永远不应该有产品&#34;灯泡&#34;和集团&#34;建设&#34;。

如果您可以在ProductLine和Group之间加入,那么报告的Group-&gt; ProductLine层次结构将被修复,您可以左键加入有关订单的信息以获得销售价值。

可能有办法解决这个问题,但首先尝试在两个表之间建立直接链接。

编辑 - 从集团加入产品线后

好的,新查询是一个好的开始: - )

现在将其分成两位:一组用于组和产品系列(下面的t1),另一组用于订单信息(下面的t2)。

这样的东西
select * from
(
select ca.pline_ID, ca.pline_desc, m.major_grp_desc from
CATEGORY ca
full join major_grp m on ca.major_grp = m.major_grp
) t1
left outer join
(
select ol.pline_num, SUM(ol.PRICE_EXT) from
[order_line] ol 
full join [ORDER]  o on ol.order_num = o.order_num
left outer join customer c on o.cust_num = c.cust_no 
left outer join salesman s on c.slsm2_num = s.slsm_num
where c.SLSM2_NUM IN (@Salesrep) 
group by ol.pline_num
) t2
ON t1.pline_ID = t2.pline_num

我遗漏了t2的大部分逻辑,但是首先尝试使用简单的t2,以便显示所有产品和组,然后根据需要添加更多条件和联合。

这是未经测试的,请记住,您可以单独运行t1和t2的子查询(例如,选择... m.major_grp)来调试它们。