简体   繁体   English

根据数字报告的SSRS步进

[英]SSRS Stepped reported based on number

Using SSRS with SQL Server 2008 R2 (Visual Studio environment). 将SSRS与SQL Server 2008 R2一起使用(Visual Studio环境)。

I am trying to produce a stepped down report based on a level/value in a table on sql server. 我试图基于sql server上的表中的级别/值生成逐步降低的报告。 The level act as a indent position with sort_value been the recursive parent in the report. 该级别用作缩进位置,sort_value是报表中的递归父级。

Sample of table in SQL Server: SQL Server中的表示例:

样品表视图

Sample of output required 所需输出样本

样本报告输出视图

OK, I've come up with a solution but please note the following before you proceed. 好的,我想出了一个解决方案,但是在继续之前, 请注意以下几点 1. The process relies on the data being in the correct order, as per your sample data. 1.该过程取决于您的样本数据的正确顺序。 2. If this is your real data structure, I strongly recommend you review it. 2.如果这是您的真实数据结构,强烈建议您复查一下。

OK, So the first things I did was recreate your table exactly as per example. 好的,所以我要做的第一件事就是完全按照示例重新创建表。 I called the table Stepped as I couldn't think of anything else! 我叫桌Stepped因为我想不出其他任何东西!

The following code can then be used as your dataset in SSRS but you can obviously just run the T-SQL directly to see the output. 下面的代码可以用作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

The output looks like this... 输出看起来像这样...

在此处输入图片说明

You may also want to consider swapping out the final statement for this. 您可能还需要考虑换成最终声明。

-- 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

As this will give you a single column for 'product' indented like this. 因为这会给您一个缩进“产品”的单列,如下所示。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM