跟之前有朋友提问过的需求应该是相似的,但是回答的几种解决办法我测试了结果有些不正确
https://bbs.csdn.net/topics/390714141我要从中间删除一些虚拟件,但是这些虚拟件下面已经包含了一些零件和材料,所以这部分要向上提高层级、合并数量
新手,麻烦大家
SELECT [end2id]--id
      ,[end1id]--上级id
      ,[end2basname]--名称
      ,[macdsscode]--单位
      ,[quantity]--用量      ,[property]--属性,c为正常,v为虚拟件
FROM [dbo].[PLMBOM]
USE [cs管理]
GO
/****** Object:  Table [dbo].[PLMBOM]    Script Date: 2018/6/13 14:39:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PLMBOM](
[end1id] [varchar](255) NULL,
[end1name] [varchar](255) NULL,
[end1basname] [varchar](255) NULL,
[itemspecification] [varchar](255) NULL,
[macdcode] [varchar](255) NULL,
[end2id] [varchar](255) NULL,
[end2name] [varchar](255) NULL,
[end2basname] [varchar](255) NULL,
[itemspecification2] [varchar](255) NULL,
[macdsscode] [varchar](255) NULL,
[quantity] [varchar](255) NULL,
[sequence] [varchar](255) NULL,
[mainpart] [varchar](255) NULL,
[property] [varchar](255) NULL,
[propertyint] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'1', N'0010', N'118000003', N'c', 3)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'213000025', N'一级半成品属性', N'锅炉底座', N'一级部件', N'PCS', N'1', N'0020', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'217000003', N'一级半成品属性', N'前保温结构', N'一级部件', N'PCS', N'2', N'0030', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'214000027', N'一级半成品属性', N'外包皮', N'一级部件', N'PCS', N'1', N'0040', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'215000015', N'一级半成品属性', N'平台扶梯', N'一级部件', N'PCS', N'1', N'0050', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'118000003', N'成品', N'卧式电蒸汽锅炉', N'WDR3.0-1.0-D', N'PCS', N'311002043', N'非一级半成品属性', N'侧面保温结构', N'一级部件', N'PCS', N'1', N'0060', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'311000983', N'非一级半成品属性', N'电热管座', N'组件', N'PCS', N'86', N'0010', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'311001922', N'非一级半成品属性', N'管板', N't16', N'PCS', N'2', N'0020', N'118000003', N'c', 3)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'311001923', N'非一级半成品属性', N'卧式锅壳', N't12', N'PCS', N'1', N'0030', N'118000003', N'c', 3)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'301013237', N'非一级半成品属性', N'管座DN20', N'20-16', N'PCS', N'2', N'0040', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'301013047', N'非一级半成品属性', N'吊耳', N'组件', N'PCS', N'2', N'0050', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'301013127', N'非一级半成品属性', N'人孔300x400-1.25(外购)', N'300*400(外购)', N'PCS', N'1', N'0060', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'301013244', N'非一级半成品属性', N'管座DN100', N'100-16', N'PCS', N'1', N'0070', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'303216690', N'非一级半成品属性', N'汽水分离器', N'', N'PCS', N'1', N'0080', N'118000003', N'c', 3)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'301013240', N'非一级半成品属性', N'管座DN40', N'40-16', N'PCS', N'3', N'0090', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'321000045', N'通用件', N'角撑板', N't16', N'PCS', N'8', N'0100', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'311001976', N'非一级半成品属性', N'角撑板', N't16', N'PCS', N'4', N'0110', N'118000003', N'v', 4)
INSERT [dbo].[PLMBOM] ([end1id], [end1name], [end1basname], [itemspecification], [macdcode], [end2id], [end2name], [end2basname], [itemspecification2], [macdsscode], [quantity], [sequence], [mainpart], [property], [propertyint]) VALUES (N'211000041', N'一级半成品属性', N'锅炉本体', N'一级部件', N'PCS', N'311001924', N'非一级半成品属性', N'水位表管座', N'组件', N'PCS', N'2', N'0120', N'118000003', N'v', 4)
GO

解决方案 »

  1.   

    怎么合并,你这些数据中,要删掉哪些,哪些数量要增加 ? 用 excel 画一下。
      

  2.   

    怎么上传不了图片,抱歉,我表述不是太清楚,我的需求应该和之前问题楼主是一样的,end1id列就是上级id,property列表示属性(v为虚拟件)
      

  3.   

    ;WITH t AS 
    (SELECT end2id,end1id,end2basname,CAST(quantity AS INT) quantity,macdsscode FROM PLMBOM WHERE end2id='211000041'
      UNION ALL
     SELECT a.end2id,t.end2id,a.end2basname,CAST(t.quantity AS INT)*CAST(a.quantity AS INT) quantity,a.macdsscode
        FROM t,PLMBOM a WHERE t.end2id=a.end1id)
    SELECT t.* FROM t LEFT JOIN PLMBOM b ON t.end2id=b.end1id
      WHERE b.end1id IS NULL 
    理论上,这个语句可以查询出某个物料的所有零件和原材料,如果半成品还能分解的话,会一只分解下去,不过你的数据中,半成品下面没有了,就只能分解到这些半成品了
      

  4.   

    看你的截图,好像只是输入单个主件编码进行报表查询。
    可以进行存储过程带主件编码参数处理:
    按照单个查询全部展开结果到临时表之后,然后把虚拟键部分delete,然后进行提高层级等update操作。最后select临时表。方法虽然笨了一些,但思路清晰。