Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 根据数量报告SSR_Sql Server_Visual Studio_Reporting Services_Ssrs 2008 R2_Business Intelligence - Fatal编程技术网

Sql server 根据数量报告SSR

Sql server 根据数量报告SSR,sql-server,visual-studio,reporting-services,ssrs-2008-r2,business-intelligence,Sql Server,Visual Studio,Reporting Services,Ssrs 2008 R2,Business Intelligence,将SSRS与SQL Server 2008 R2(Visual Studio环境)一起使用 我正在尝试根据sql server上的表中的级别/值生成一个分级报告。该级别充当缩进位置,sort_值是报表中的递归父级 SQL Server中的表示例: 所需输出样本 好的,我已经想出了一个解决方案,但是在继续之前请注意以下几点。 1.根据您的示例数据,该过程依赖于数据的正确顺序。 2.如果这是您真正的数据结构,我强烈建议您查看它 好的,我做的第一件事就是完全按照示例重新创建表。我打电话给那张桌子,因

将SSRS与SQL Server 2008 R2(Visual Studio环境)一起使用

我正在尝试根据sql server上的表中的级别/值生成一个分级报告。该级别充当缩进位置,sort_值是报表中的递归父级

SQL Server中的表示例:

所需输出样本


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

好的,我做的第一件事就是完全按照示例重新创建表。我打电话给那张桌子,因为我想不出还有什么

下面的代码可以用作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
输出如下所示

您可能还想考虑交换最后的语句。

-- 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
因为这将为您提供一列缩进如下的“产品”。

您的问题是水龙头和配件都是产品。这使得您不能以这种方式拆分它。这是表中数据的实际格式还是另一个查询的结果。如果它是另一个查询的结果,您可以发布原始数据的样本吗。我认为,您尝试做的是可能的,如果我们有更好的结构化数据,可能会容易得多。