还是BOM表穿透半成品查询子项的问题
是SQL2000,不是SQL2005,所以不能使用WITH AS 如何实现?
原始表
FUID FUMS ZIID ZIMS QTY LVL
00-001 cp 01-001 bcp1 1 0
01-001 bcp1 05-001 ycl1 0.5 1
01-001 bcp1 01-002 bcp2 2 1
01-002 bcp2 06-001 ycl2 1 2
01-002 bcp2 06-002 ycl3 3 2
结果表
FUID FUMX ZIID ZIMS QTY
00-001 cp 05-001 ycl1 0.5
00-001 cp 06-001 ycl2 2
00-001 cp 06-002 ycl3 6
如何穿透半成品直接取出原材料的用量
生成表语句
IF OBJECT_ID ( '[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([fuID] [nvarchar] ( 10), [fums] [nvarchar]( 10), [ziID][nvarchar] (10 ), [zims] [nvarchar]( 10 ),[qty] [decimal], [lvl] [int])
INSERT INTO [tb]
SELECT '00-001' , 'cp ' ,'01-001' , 'bcp1 ' ,'1' , '0' UNION ALL
SELECT '01-001' , 'bcp1 ' ,'05-001' , 'ycl1 ' ,'0.5' , '1' UNION ALL
SELECT '01-001' , 'bcp1 ' ,'01-002' , 'bcp2 ' ,'2' , '1' UNION ALL
SELECT '01-002' , 'bcp2 ' ,'06-001' , 'ycl2 ' ,'1' , '2' UNION ALL
SELECT '01-002' , 'bcp2 ' ,'06-002' , 'ycl3 ' ,'3' , '2'
------解决方案--------------------试试这个:
USE tempdb
GO
IF OBJECT_ID ( '[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]
(
[fuID] [nvarchar](10),
[fums] [nvarchar](10),
[ziID] [nvarchar](10),
[zims] [nvarchar](10),
[qty] [decimal](10,2),
[lvl] [int]
)
INSERT INTO [tb]
SELECT '00-001' , 'cp ' ,'01-001' , 'bcp1 ' , 1 , 0 UNION ALL
SELECT '01-001' , 'bcp1 ' ,'05-001' , 'ycl1 ' ,0.5 , 1 UNION ALL
SELECT '01-001' , 'bcp1 ' ,'01-002' , 'bcp2 ' , 2 , 1 UNION ALL
SELECT '01-002' , 'bcp2 ' ,'06-001' , 'ycl2 ' , 1 , 2 UNION ALL
SELECT '01-002' , 'bcp2 ' ,'06-002' , 'ycl3 ' , 3 , 2
GO
SELECT * FROM tb
GO
SET NOCOUNT ON<