I'm having 3 tables: PRICE, PRICE_PRODUCT, CHANGEABLE_PRICES. (I cannot change the data modeling in the database)
the issue is that I need to update sometimes table CHANGEABLE_PRICES based on PRICE table. I can link these tables via PRICE_PRODUCT table and date fields (Valid_date and date) which is present in both of them. I can link PRICE and PRICE_PRODUCT based on PriceID. and PRICE_PRODUCT and CHANGEABLE_PRICES based on ProductID,CompanyID,DeliveryID. Table CHANGEABLE_PRICES and PRICE I can link only via Valid_date and date.
CREATE TABLE [dbo].[PRICE](
[PriceId] [int] NOT NULL,
[Valid_date] [datetime] NOT NULL,
[Price_Value] [decimal](18, 0) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO PRICE VALUES (21, '2018-05-31', 45)
INSERT INTO PRICE VALUES (22, '2018-05-06', 10)
CREATE TABLE [dbo].[PRICE_PRODUCT](
[PriceID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[CompanyID] [nvarchar](50) NOT NULL,
[DeliveryID] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO PRICE_PRODUCT VALUES (21, 1, 'A', '001')
INSERT INTO PRICE_PRODUCT VALUES (22, 1, 'A', '001')
CREATE TABLE [dbo].[CHANGEABLE_Prices](
[ProductID] [int] NOT NULL,
[CompanyID] [nvarchar](50) NOT NULL,
[DeliveryID] [nvarchar](50) NOT NULL,
[Date] [datetime] NOT NULL,
[Price_Value] [decimal](18, 0) NOT NULL
) ON [PRIMARY]
GO
I was trying to do something like this:
update CHANGEABLE_Prices set Price_Value = (select p.Price_Value from Price p inner join PRICE_PRODUCT pp
on p.PriceId = pp.PriceId, CHANGEABLE_PRICES cp where cp.ProductID = CHANGABLE_Prices.ProductID
and cp.CompanyID = CHANGABLE_Prices.CompanyID
and cp.DeliveryID = CHANGABLE_Prices.DeliveryID
and p.Valid_date = CHANGABLE_Prices.date)
But i'm getting error:
The multi-part identifier "CHANGABLE_Prices.ProductID" could not be bound.
and have no idea how to handle this issue.
any ideas? anyone, please?
thanks in advance.