Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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 材料工程量清单_Sql_Sql Server 2008 - Fatal编程技术网

Sql 材料工程量清单

Sql 材料工程量清单,sql,sql-server-2008,Sql,Sql Server 2008,我想列出材料清单的总额。我制作了一些示例代码: 正如您所看到的,由于递归级别,物料清单正在重复 所以,我希望结果是: ┌────────────┬───────────┬────────────┐ │家长党│儿童党│子零件数量│ ├────────────┼───────────┼────────────┤ │淋浴-A│玻璃-A│2.│ ├────────────┼───────────┼────────────┤ │玻璃-A│某样东西│10│ ├────────────┼─────────

我想列出材料清单的总额。我制作了一些示例代码:

正如您所看到的,由于递归级别,物料清单正在重复

所以,我希望结果是:


┌────────────┬───────────┬────────────┐
│家长党│儿童党│子零件数量│
├────────────┼───────────┼────────────┤
│淋浴-A│玻璃-A│2.│
├────────────┼───────────┼────────────┤
│玻璃-A│某样东西│10│
├────────────┼───────────┼────────────┤
│某样东西│某物-B│30│
└────────────┴───────────┴────────────┘

我正在考虑按最高递归级别分组,但感觉很混乱。

是一个可能有用的解决方案:

; with cte_gettopparents as 
(
  select a.*, ROW_NUMBER() over (order by a.ParentPartId) as ordr
  from   BillOfMaterial a 
  where  not exists
  (
    select 1 
    from   BillOfMaterial b
    where  b.ChildPartId = a.ParentPartId
  )
)
--select * from cte_gettopparents
,
cte_getallchildren as
(
  select *
  from   cte_gettopparents
  union  all
  select a.ParentPartId, a.ChildPartId ,a.ChildPartQty * b.ChildPartQty,b.ordr
  from   BillOfMaterial a join cte_getallchildren b
  on     b.ChildPartId = a.ParentPartId
)

select ParentPartId, ChildPartId ,ChildPartQty, ordr as [hierarchyid]
from   cte_getallchildren
order  by ordr,ChildPartId
如果希望得到不同的结果,请使用:

select Distinct ParentPartId, ChildPartId ,ChildPartQty
from   cte_getallchildren
order  by ChildPartQty