【问题标题】:Merging rows on SSRS合并 SSRS 上的行
【发布时间】:2018-09-13 02:15:12
【问题描述】:

我的原始数据返回到 SSRS。

IF OBJECT_ID('tempdb..#tmpElections') IS NOT NULL 
DROP TABLE #tmpElections
create table #tmpElections
(
ClientId int,
MaterialType varchar(50),
QtyReq int,
QtySent int
)

insert into #tmpElections values (1,'MM1',100,50)
insert into #tmpElections values (2,'MM2',200,50)
insert into #tmpElections values (2,'MM2',200,25)
insert into #tmpElections values (3,'MM3',300,50)
insert into #tmpElections values (3,'MM3',300,150)
insert into #tmpElections values (3,'MM3',300,100)
insert into #tmpElections values (4,'MM4',400,300)
insert into #tmpElections values (4,'MM4',400,100)
select * from #tmpElections

在报告中,status = partial,如果 QtySent

我的 ssrs 报告应显示如下,合并/空白行单元格, 具有相同的 Clientid、materialType 和 status = 'Full'。应显示 QtySent 列。

Desired Report Sample 什么是最好的方法以及如何达到这个结果。 这应该在 T-SQL 还是 SSRS 中处理。

每个组内报告中的黄色突出显示单元格应为空白。 Sample Report

【问题讨论】:

标签: tsql reporting-services


【解决方案1】:

我会使用子查询来汇总您的 QtySent 以进行比较,并使用 CASE 来分配状态文本值。剩下的只是 SSRS 格式。

SELECT
  e.*
 ,CASE
    WHEN s.TotSent = e.QtyReq THEN 'Full'
    ELSE 'Partial'
  END AS [Status]
FROM
  #tmpElections AS e
  LEFT JOIN
    (
      SELECT
        e2.ClientId
       ,e2.MaterialType
       ,SUM(e2.QtySent) AS TotSent
      FROM
        #tmpElections AS e2
      GROUP BY
        e2.ClientId
       ,e2.MaterialType
    ) AS s
      ON
      s.ClientId = e.ClientId
      AND s.MaterialType = e.MaterialType;

结果集:

+----------+--------------+--------+---------+---------+
| ClientId | MaterialType | QtyReq | QtySent | Status  |
+----------+--------------+--------+---------+---------+
|        1 | MM1          |    100 |      50 | Partial |
|        2 | MM2          |    200 |      50 | Partial |
|        2 | MM2          |    200 |      25 | Partial |
|        3 | MM3          |    300 |      50 | Full    |
|        3 | MM3          |    300 |     150 | Full    |
|        3 | MM3          |    300 |     100 | Full    |
|        4 | MM4          |    400 |     300 | Full    |
|        4 | MM4          |    400 |     100 | Full    |
+----------+--------------+--------+---------+---------+

【讨论】:

    【解决方案2】:

    你快到了.. 我要做的是添加一个 case 语句来确定状态:

       select ClientId,MaterialType, max(QtyReq) as qtyreq, sum(QtySent) as qtysent
     , case when sum(QtySent)<max(QtyReq)  then 'Partial' else 'Full' end as [status] 
     from #tmpElections
    
     group by 
    
     ClientId
     ,MaterialType
    

    然后在您的报告中......您只需对图像描述中显示的前三列进行分组......然后将其余部分作为详细信息

    【讨论】:

    • case 语句会将所有行标记为部分行。在示例行中,clientid 为 3 和 4 的行的状态应为“已满”。
    • 查看我编辑的代码..如果您想了解所有详细信息..然后您可以在客户端 ID 和材料类型上再次加入表格并返回所有内容
    • SQL 部分看起来不错。我对报告的前 3 列进行了分组。在组行中,我需要在除组中的最后一行之外的所有行上隐藏这 3 列。我怎样才能实现这种显示。
    【解决方案3】:

    感谢大家的 cmets 和解决方案。我能够解决我的问题如下。

    Create procedure dbo.TestRptSample
    as
    begin
    
    
    create table #tmpElections
    (
    ClientId int,
    MaterialType varchar(50),
    QtyReq int,
    QtySent int,
    SentDate datetime
    )
    
    insert into #tmpElections values (1,'MM1',100,50,'02/01/2018')
    insert into #tmpElections values (2,'MM2',200,50,'02/01/2018')
    insert into #tmpElections values (2,'MM2',200,25,'03/01/2018')
    insert into #tmpElections values (3,'MM3',300,50,'02/01/2018')
    insert into #tmpElections values (3,'MM3',300,150,'02/15/2018')
    insert into #tmpElections values (3,'MM3',300,100,'03/01/2018')
    insert into #tmpElections values (4,'MM4',400,300,'02/01/2018')
    insert into #tmpElections values (4,'MM4',400,100,'03/01/2018')
    
    create table #tmpFinal
    (
    ClientId int,
    MaterialType varchar(50),
    QtyReq int,
    QtySent int,
    SentDate datetime,
    mStatus varchar(100),
    )
    
    Insert into #tmpFinal
    select b.*,a.status
    from
    (
    select ClientId,MaterialType, max(QtyReq) as qtyreq, sum(QtySent) as qtysent
     , case when sum(QtySent)<max(QtyReq)  then 'Partial' else 'Full' end as [status] 
     from #tmpElections
     group by 
     ClientId
     ,MaterialType
     ) A
     inner join #tmpElections B on a.ClientId = b.ClientId and a.MaterialType = b.MaterialType;
    
     with x as
     (
     select *,
     ROW_NUMBER() over (partition by clientId,materialType,qtyReq
     order by sentdate) as Rowno
     from #tmpFinal
     )
    
    
      select * 
      ,max(rowno) over (partition by clientId,materialType,qtyReq) as MaxRow
      from x
      order by clientId ,sentdate
    
    end
    

    使用带有 row_number 的过程在按集分组内生成行号。 在报表上,在行文本框的可见性表达式中,使用以下表达式来显示或隐藏该列。

    iif(Fields!mStatus.Value="Full" and Fields!Rowno.Value Fields!MaxRow.Value ,True,False)

    【讨论】:

      猜你喜欢
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多