0

报告的SSRS逐步使用SSRS与SQL Server 2008 R2(Visual Studio环境)一起使用。根据编号

我想在sql服务器上根据一个表中的级别/值产生一个减少的报告。该级别充当sort_value的缩进位置,是报表中的递归父级。在SQL Server

样品表:

Sample table View

输出的样品所需

Sample Report output view

+0

你的问题是,龙头和配件都是一个产品。这使得它不能以这种方式拆分它。 – Snowlockk

+0

这是表中数据的实际格式还是这是另一个查询的结果。如果是另一个查询的结果,您可以发布原始数据的样本。如果我们拥有更好的结构化数据,那么您想要做的事情是可能的,我想可能会更容易。 –

回答

0

好了,我想出了一个解决方案,但请注意以下在你继续之前。 1.该过程依赖于正确顺序的数据,根据您的样本数据。 2.如果这是您的真实数据结构,我强烈建议您检查一下。

好的,所以我做的第一件事就是按照示例重新创建表格。我把表Stepped称为我无法想到的其他东西!

以下代码可以用作SSRS中的数据集,但显然您可以直接运行T-SQL来查看输出。

-- Create a copy of the data with a row number. This means the input data MUST be in the correct order. 
DECLARE @t TABLE(RowN int IDENTITY(1,1), Sort_Order int, [Level] int, Qty int, Currency varchar(20), Product varchar(20)) 

INSERT INTO @t (Sort_Order, [Level], Qty, Currency, Product) 
    SELECT * FROM Stepped 

-- Update the table so each row where the sort_order is NULL will take the sort order from the row above 
UPDATE a SET Sort_Order = b.Sort_Order 
FROM @t a 
    JOIN @t b on a.RowN = b.rowN+1 
WHERE a.Sort_Order is null and b.Sort_Order is not null 

-- repeat this until we're done. 
WHILE @@ROWCOUNT >0 
    BEGIN 
     UPDATE a SET Sort_Order = b.Sort_Order 
      FROM @t a 
       JOIN @t b on a.RowN = b.rowN+1 
      WHERE a.Sort_Order is null and b.Sort_Order is not null 
    END 

-- Now we can select from our new table sorted by both sort oder and level. 
-- We also separate out the products based on their level. 
SELECT 
     CASE Level WHEN 1 THEN Product ELSE NULL END as ProdLvl_1 
     , CASE Level WHEN 2 THEN Product ELSE NULL END as ProdLvl_2 
     , CASE Level WHEN 3 THEN Product ELSE NULL END as ProdLvl_3 
     , QTY 
     , Currency 
    FROM @t s 
    ORDER BY Sort_Order, Level 

输出看起来像这样...

enter image description here

您也可以考虑换出最后陈述这一点。

-- Alternatively use this style and use a single column in the report. 
-- This is better if the number of levels can change. 
SELECT 
     REPLICATE('--', Level-1) + Product as Product 
     , QTY 
     , Currency 
    FROM @t s 
    ORDER BY Sort_Order, Level 

因为这会给你一个'产品'列这样的缩进。 enter image description here