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