2016-06-10 25 views
0

我有这个存储过程,它提供了一些库存数据和标准格式,并没有考虑任何父子关系。这是当前数据以及其他许多列的视图:亲子数据对齐

我试图在此报告中引入父/子关系。因此我修改了所有查询以引入所有父/子数据。数据需要以特定方式呈现。如下图:

enter image description here

基本上,需要列出每个家长首先其次是所有的子记录。然后找到所有孩子的这些记录显示在父行上。

the min of all child "1st Receipt Date", 
max of all child "Last Receipt Date", 
Sum of all child "On Hand" 
Sum of all child "Sales Unit" 
Mths Supply will be calculated from the total On Hand and Sales Units 

enter image description here

我不知道如何安排这样的数据。将不胜感激某些方向。

在此先感谢。

+0

我在销售单位想出160。这不是一笔钱吗?你怎么计算#Mths Supply? –

+0

划伤160问题,我有一个错字,The Months Supply问题仍然存在 –

回答

1
-- This Section is Just to Stage some Sample Data 
-------------------------------------------------------------------------------------------------------- 
Declare @Inv table (SKU int,FirstReceiptDate Date,LastReceiptDate Date,OnHand int,SalesUnits int) 
Insert Into @Inv (SKU,FirstReceiptDate,LastReceiptDate,OnHand,SalesUnits) values 
(456,'2014-03-15','2014-12-14',0,15), 
(789,'2014-05-30','2014-12-15',10,35), 
(321,'2014-07-31','2016-03-16',112,60) 

Declare @Hier table (SKU int,PtSKU int,PS int,Title varchar(50)) 
Insert into @Hier (SKU,PtSKU,PS,Title) values 
(123,0,0,'SKU Tile 123'), 
(456,123,10,'Some Other Title SKU 456'), 
(789,123,20,'This is the Title for Title SKU 786'), 
(321,123,30,'Finally Tile 321') 


-- This Section Builds the Hierarchy with Range Keys Hierarchies can be variable depth 
-- My hierarchies are pretty static so they are rebuilt as needed 
-- The real power is using the range key. You can aggregate data without 
-- a recursive query. 
-- I should not that I added a Presentation Sequence (PS) and Title to the Hierarcy 
-- The PS is used to control the presentation order. This can be alphabetical as well 
-------------------------------------------------------------------------------------------------------- 
;With cteOH (SKU,PtSKU,Lvl,PS,SortSeq) as 
    (
     Select SKU,PtSKU,Lvl=1,PS,cast([dbo].[udf-Str-PadL](PS,0,6) +':' +[dbo].[udf-Str-PadL](SKU,0,6) + '/' as varchar(500)) from @Hier where PtSKU=0 
     Union All 
     Select h.SKU,h.PtSKU,cteOH.Lvl+1,h.PS,SortSeq=cast(cteOH.SortSeq + [dbo].[udf-Str-PadL](H.PS,0,6) +':' +[dbo].[udf-Str-PadL](H.SKU,0,6) + '/' as varchar(500)) FROM @Hier h INNER JOIN cteOH ON h.PtSKU = cteOH.SKU 
    ) 
    ,cteR1 as (Select SKU,SortSeq,R1=Row_Number() over (Order by SortSeq) From cteOH) 
    ,cteR2 as (Select A.SKU,R2 = max(B.R1) From cteOH A Join cteR1 B on (B.SortSeq Like A.SortSeq+'%') Group By A.SKU) 
    Select B.R1 
      ,C.R2 
      ,A.Lvl 
      ,A.SKU 
      ,A.PtSKU 
      ,A.PS 
      ,T.Title 
    Into #TempOH 
    From cteOH A 
    Join cteR1 B on (A.SKU=B.SKU) 
    Join cteR2 C on (A.SKU=C.SKU) 
    Join @Hier T on (A.SKU=T.SKU) 
    Order By B.R1 


-- This Section illustrates how to aggregate data via the range keys 
-------------------------------------------------------------------------------------------------------- 
Select A.* 
     ,FirstReceiptDate  = min(B.FirstReceiptDate) 
     ,LastReceiptDate  = max(B.LastReceiptDate) 
     ,OnHand     = sum(B.OnHand) 
     ,SalesUnits    = sum(B.SalesUnits) 
     ,MonthsSupply   = cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money) 
     ,FamilyFirstReceiptDate = First_Value(min(B.FirstReceiptDate)) Over (Order By A.R1) 
     ,FamilyLastReceiptDate = First_Value(max(B.LastReceiptDate)) Over (Order By A.R1) 
     ,FamilyOnHand   = First_Value(sum(B.OnHand))   Over (Order By A.R1) 
     ,FamilySalesUnits  = First_Value(sum(B.SalesUnits))  Over (Order By A.R1) 
     ,FamilyMonthsSupply  = First_Value(cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money))  Over (Order By A.R1) 
From #TempOH A 
Join (Select _R1=B.R1,A.* From @Inv A Join #TempOH B on A.SKU=B.SKU) B on _R1 between A.R1 and A.R2 
Group By A.R1,A.R2,A.Lvl,A.SKU,A.PtSKU,A.PS,A.Title 
Order By A.R1 

返回

R1     R2     Lvl   SKU   PtSKU  PS   Title            FirstReceiptDate LastReceiptDate OnHand  SalesUnits MonthsSupply   FamilyFirstReceiptDate FamilyLastReceiptDate FamilyOnHand FamilySalesUnits FamilyMonthsSupply 
-------------------- -------------------- ----------- ----------- ----------- ----------- -------------------------------------------------- ---------------- --------------- ----------- ----------- --------------------- ---------------------- --------------------- ------------ ---------------- --------------------- 
1     4     1   123   0   0   SKU Tile 123          2014-03-15  2016-03-16  122   110   13.3091    2014-03-15    2016-03-16   122   110    13.3091 
2     2     2   456   123   10   Some Other Title SKU 456       2014-03-15  2014-12-14  0   15   0.00     2014-03-15    2016-03-16   122   110    13.3091 
3     3     2   789   123   20   This is the Title for Title SKU 786    2014-05-30  2014-12-15  10   35   3.4286    2014-03-15    2016-03-16   122   110    13.3091 
4     4     2   321   123   30   Finally Tile 321         2014-07-31  2016-03-16  112   60   22.40     2014-03-15    2016-03-16   122   110    13.3091 

所需的功能

CREATE FUNCTION [dbo].[udf-Str-PadL] (@Value varchar(50),@Pad varchar(10) = '0',@Len int = 10) 

-- Syntax : Select [dbo].[udf-Str-PadL](25,0,10) 
-- Syntax : Select [dbo].[udf-Str-PadL](25,'-',6) 

Returns varchar(50) 
AS 
    BEGIN 
    Return right(concat(Replicate(@Pad,@Len),@Value),@Len) 
    END 
+0

嗨,John,感谢您对解决方案的回应。你能否解释一下这个“[dbo]。[udf-Str-PadL]”是你正在调用的函数吗? – Eclipse

+0

我的歉意。我没有包含该功能。我将修改我的答案以包含该功能。 –

+0

感谢您添加该功能。我的表结构看起来有点不同,并且有一个非常复杂的查询来提取当前数据。向您展示我现在包含数据的最佳方式是什么? – Eclipse