【问题标题】:parent-child data alignment父子数据对齐
【发布时间】:2016-06-10 21:52:52
【问题描述】:

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

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

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

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

我不知道如何以这种方式排列数据。将不胜感激一些方向。

提前致谢。

【问题讨论】:

  • 我想出 160 个销售单位。这不是总和吗?还有你如何计算#Mths Supply?
  • 解决第 160 个问题,我有一个错字,月供应问题仍然有效

标签: sql sql-server-2008 parent-child


【解决方案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

【讨论】:

  • 嗨,约翰,感谢您对解决方案的回复。你能解释一下这个“[dbo].[udf-Str-PadL]”是你调用的函数吗?
  • 我很抱歉。我未能包含该功能。我将修改我的答案以包含该功能。
  • 感谢您添加该功能。我的表结构看起来有点不同,并且有一个非常复杂的查询来提取当前数据。向您展示我现在拥有的数据(包括数据)的最佳方式是什么?
  • 让我们分小步进行。要记住的是,等级制度为王。要聚合的数据是次要的,我们稍后会解决这个问题。所以我的建议是用一些示例数据向我展示你的 SKU 结构
  • 嗨 John,我在此处添加了我的示例表和当前的最终查询以在此处获取结果集:pastebin.com/rKHtMWQK 我试图解释一些临时表,但如果您对数据有任何疑问,请在这里提问,我会立即回复。您会看到使用了很多临时表,并且我创建了一些其他临时表来将父/子数据与非父/子项分开。我的计划是在我从两个单独的查询中获取数据集后合并。您可能会看到一些未在创建脚本中定义的字段,因为它们不是关键字段,并且某些名称可能不匹配。
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多