成本表基于成本和成本。物品表

时间:2014-08-06 12:12:06

标签: sql sql-server sql-server-2008

我想创建&更新“ReplacementCostTable”,它取决于“PartCostTable”&中的值。 “ItemTable”有很多项目,每个项目有三个部分。我正在使用SQL Server 2008 R2。非常感谢您的帮助。

CREATE TABLE [dbo].[PartCostTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PartNo] [nvarchar](15) NULL,
[Cost] [smallmoney] NULL,
 CONSTRAINT [PK_PartCostTable] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS   = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[ItemTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ItemNo] [nvarchar](15) NULL,
[Part1Cond] [nvarchar](15) NULL,
[Part2Cond] [nvarchar](185) NULL,
[Part3Cond] [nvarchar](15) NULL,
 CONSTRAINT [PK_PartTable] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

我试过这个,但结果不像预期的ReplacementCostTable!

SELECT a.itemno, 
b.Cost AS Part1,
b.Cost AS Part2,
b.Cost AS Part3

FROM dbo.ItemTable a, dbo.PartCostTable b

 where a.Part1Cond like 'Replace'
 or
  a.Part2Cond like 'Replace'
  or
  a.Part3Cond like 'Replace'

GO

![ItemTable][1]

![PartCostTable][2]

![ReplacementCostTable][3]

修改

以下是OP在评论中提供的示例数据。

INSERT INTO dbo. PartCostTable (PartNo,Cost) VALUES ('PART1',10.00), ('PART2',15.00), ('PART3',20.00)

INSERT INTO dbo.ItemTable (ItemNo,Part1Cond,Part2Cond,Part3Cond) VALUES ('2M3-PS-18','GOOD','REPLACE','GOOD'), ('2M3-PS-20','GOOD','REPLACE','GOOD'), ('2M3-PS-22','GOOD','REPLACE','GOOD') 

1 个答案:

答案 0 :(得分:0)

让我们制作一些测试数据:

DECLARE @PartCostTable TABLE
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [PartNo] [nvarchar](15) NULL,
    [Cost] [smallmoney] NULL
)

DECLARE @ItemTable TABLE
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ItemNo] [nvarchar](15) NULL,
    [Part1Cond] [nvarchar](15) NULL,
    [Part2Cond] [nvarchar](185) NULL,
    [Part3Cond] [nvarchar](15) NULL
)


INSERT INTO @PartCostTable 
(PartNo,Cost) 
VALUES 
('PART1',10.00), 
('PART2',15.00), 
('PART3',20.00);

INSERT INTO @ItemTable 
(ItemNo,Part1Cond,Part2Cond,Part3Cond) 
VALUES 
('2M3-PS-18','GOOD','REPLACE','GOOD'), 
('2M3-PS-20','GOOD','REPLACE','GOOD'), 
('2M3-PS-22','GOOD','REPLACE','GOOD');

以下是我对op正在尝试的内容的猜测:

SELECT 
    it.ID,
    it.ItemNo,
    CASE WHEN it.Part1Cond = 'REPLACE' THEN pct1.Cost ELSE 0 END Cost1,
    CASE WHEN it.Part2Cond = 'REPLACE' THEN pct2.Cost ELSE 0 END Cost2,
    CASE WHEN it.Part3Cond = 'REPLACE' THEN pct3.Cost ELSE 0 END Cost3,
    (CASE WHEN it.Part1Cond = 'REPLACE' THEN  pct1.Cost ELSE 0 END) +
    (CASE WHEN it.Part2Cond = 'REPLACE' THEN  pct2.Cost ELSE 0 END) +
    (CASE WHEN it.Part3Cond = 'REPLACE' THEN  pct3.Cost ELSE 0 END) TotalCost
FROM @ItemTable it
LEFT JOIN @PartCostTable pct1
    ON pct1.PartNo = 'PART1'
LEFT JOIN @PartCostTable pct2
    ON pct2.PartNo = 'PART2'
LEFT JOIN @PartCostTable pct3
    ON pct3.PartNo = 'PART3'

这是输出:

ID  ItemNo  Cost1   Cost2   Cost3   TotalCost
1   2M3-PS-18   0.00    15.00   0.00    15.00
2   2M3-PS-20   0.00    15.00   0.00    15.00
3   2M3-PS-22   0.00    15.00   0.00    15.00

当您开始添加更多数据时,部件成本似乎应该有更明显的连接(恕我直言)