使用以下数据库作为我的数据来源:
CREATE DATABASE [COS Source]
GO
USE [COS Source]
GO
CREATE TABLE [Products](
[INVENTORY_CODE] [nvarchar](50),
[PRODCAT_CODE] [nvarchar](50),
[LAST_COST] [int] NULL,
[STOCK_IND] [nvarchar](255) NULL,
CONSTRAINT piv PRIMARY KEY ([INVENTORY_CODE])
)
GO
CREATE TABLE [Sales Header](
[DOC_NUMBER] [nvarchar](50),
[TRANSTYPE_CODE] [int] null ,
[REP_CODE] [nvarchar](255) NULL,
[CUSTOMER_NUMBER] [nvarchar](50) ,
[TRANS_DATE] [datetime] NULL,
[FIN_PERIOD] [datetime] NULL,
CONSTRAINT shdc PRIMARY KEY ([DOC_NUMBER])
)
GO
CREATE TABLE [Sales Line](
[DOC_NUMBER] [nvarchar](50) FOREIGN KEY REFERENCES [Sales Header]([DOC_NUMBER]),
[INVENTORY_CODE] [nvarchar](50) FOREIGN KEY REFERENCES Products([INVENTORY_CODE]),
[QUANTITY] [float] NULL,
[UNIT_SELL_PRICE] [int] NULL,
[TOTAL_LINE_PRICE] [float] NULL,
[LAST_COST] [int] NULL,
CONSTRAINT sldcic PRIMARY KEY ([DOC_NUMBER],[INVENTORY_CODE])
)
GO
CREATE TABLE [Purchases Headers](
[SUPPLIER_CODE] [nvarchar](255) NULL,
[PURCH_DOC_NO] [nvarchar](50),
[PURCH_DATE] [datetime] NULL,
CONSTRAINT phpdn PRIMARY KEY ([PURCH_DOC_NO])
)
GO
CREATE TABLE [Purchases Lines](
[PURCH_DOC_NO] [nvarchar](50) FOREIGN KEY REFERENCES [Purchases Headers]([PURCH_DOC_NO]),
[INVENTORY_CODE] [nvarchar](50) FOREIGN KEY REFERENCES [Products]([INVENTORY_CODE]),
[QUANTITY] [float] NULL,
[UNIT_COST_PRICE] [float] NULL,
[TOTAL_LINE_COST] [float] NULL,
CONSTRAINT plpdc PRIMARY KEY ([PURCH_DOC_NO],[INVENTORY_CODE])
)
GO
使用以下数据库作为插入目的地:
CREATE DATABASE [COS Target]
GO
USE [COS Target]
GO
CREATE TABLE PRODUCT_DIM (
Product_Key int IDENTITY(1,1),
Product_Old_Key varchar(255),
Unit_Sale_Price decimal,
CONSTRAINT pk_PRODUCT_DIM PRIMARY KEY (Product_Key)
)
GO
CREATE TABLE CUSTOMER_SALES_FACT (
Cust_Sales_Fact_Key int IDENTITY(1,1),
Product_Key int,
Cost_Of_Sales decimal,
CONSTRAINT pk_CUSTOMER_SALES_FACT PRIMARY KEY (Cust_Sales_Fact_Key, Product_Key),
CONSTRAINT Customer_Sale_Fact_Product_Key FOREIGN KEY (Product_Key) REFERENCES PRODUCT_DIM(Product_Key) ON DELETE CASCADE,
)
GO
我想计算每个[COS Source].dbo.[Sales Line]
的销售成本(COS),并将此值插入[COS Target].dbo.[CUSTOMER_SALES_FACT].Cost_Of_Sales
。计算每条销售线的销售成本的基本公式为:[COS Source].dbo.[Purchases Lines].[QUANTITY] * [COS Source].dbo.[Purchases Lines].[UNIT_COST_PRICE]
,或者只是[COS Source].dbo.[Purchases Lines].[TOTAL_LINE_COST]
。
使用以下查询填充[Product_Dim]
表:
Insert into dbo.PRODUCT_DIM
(
Product_Old_Key
,[Unit_Sale_Price]
)
Select distinct p.INVENTORY_CODE ,p.LAST_COST from [COS Source].dbo.Products as p
Join [COS Source].dbo.[Sales Line] as sl
on p.INVENTORY_CODE = sl.INVENTORY_CODE
Join [COS Source].dbo.[Sales Header] sh
on sl.DOC_NUMBER = sh.DOC_NUMBER
GO
到目前为止,我必须填充[COS Target].dbo.[CUSTOMER_SALES_FACT]
但尚未完成的插入查询,如下所示:
Insert into dbo.CUSTOMER_SALES_FACT
(
[Product_Key]
,[Cost_Of_Sales]
)
Select Distinct targetProduct.Product_Key, null /*insert COS here*/from [COS Source].dbo.[Sales Line] as sl
Join [COS Source].dbo.[Sales Header] as sh
on sl.DOC_NUMBER = sh.DOC_NUMBER
Join [COS Source].dbo.Products as p
on p.INVENTORY_CODE = sl.INVENTORY_CODE
Join dbo.PRODUCT_DIM as targetProduct
on [COS Target].Product_Old_Key = p.INVENTORY_CODE
Join [COS Source].dbo.[Purchases Lines] pl
on p.INVENTORY_CODE = pl.INVENTORY_CODE
Join [COS Source].dbo.[Purchases Headers] as ph
on pl.PURCH_DOC_NO = ph.PURCH_DOC_NO
where ph.PURCH_DATE <= sh.TRANS_DATE
GO
理想情况下,我希望将计算出的COS放入用于填充[COS Target].dbo.[CUSTOMER_SALES_FACT]
的插入语句中,但问题在于每个产品存在多个购买行。因此,为了创建至少部分准确的COS,我必须选择在销售线之前发生的购买行。意味着条件
WHERE [COS Source].dbo.[Sales Header].[TRANS_DATE] >= [COS Source].dbo.[Purchases Header].[PURCH_DATE]
满足。
我没有成功计算和插入COS。
我正在使用SQL Server 2008.这些表还有钱条目。
非常感谢任何帮助!
答案 0 :(得分:0)
此处缺少的信息是:对于给定销售中的给定INVENTORY_CODE(DOC_NUMBER),此次包含此INVENTORY_CODE 之前的最新购买何时销售?
一种选择是创建一个辅助视图:
CREATE VIEW dbo.[Most Recent Purchase] AS
SELECT sl.DOC_NUMBER, sl.INVENTORY_CODE, MAX(PURCH_DATE) LATEST_PURCH_DATE
FROM [COS Source].dbo.[Purchases Lines] pl
JOIN [COS Source].dbo.[Purchases Headers] ph on pl.PURCH_DOC_NO=pl.PURCH_DOC_NO
JOIN [COS Source].dbo.[Sales Line] sl ON pl.INVENTORY_CODE = sl.INVENTORY_CODE
JOIN [COS Source].dbo.[Sales Header] sh ON sh.DOC_NUMBER=sl.DOC_NUMBER
WHERE PURCH_DATE < sh.TRANS_DATE
GROUP BY sl.DOC_NUMBER, sl.INVENTORY_CODE
此视图现在将告诉您:
DOC_NUMBER INVENTORY_CODE LATEST_PURCH_DATE
1 123 2013-04-15
销售1([销售代码] .DOC_NUMBER = 1)包含INVENTORY_ID = 123的销售行。此次促销的TRANS_DATE最近一次购买INVENTORY_ID = 123的时间是2013-04-15。
现在尝试将此视图添加到您的联接中并从那里获取。