593
社区成员
发帖
与我相关
我的任务
分享
/* http://bbs.csdn.net/topics/390813196
*/
WITH store(year_month,product_code,data_type,inv) AS (
SELECT 201401,'C111','AAA',100 UNION ALL
SELECT 201401,'D112','BBB',120 UNION ALL
SELECT 201402,'C111','AAA',160 UNION ALL
SELECT 201402,'D112','BBB',220 UNION ALL
SELECT 201402,'C111','AAA',360 UNION ALL
SELECT 201402,'D112','BBB',320
)
,require(year_month,product_code,data_type,inv_f) AS (
SELECT 201401,'C111','AAA',20 UNION ALL
SELECT 201401,'D112','BBB',30 UNION ALL
SELECT 201402,'C111','AAA',20 UNION ALL
SELECT 201402,'D112','BBB',60 UNION ALL
SELECT 201403,'C111','AAA',50 UNION ALL
SELECT 201403,'D112','BBB',20 UNION ALL
SELECT 201404,'C111','AAA',20 UNION ALL
SELECT 201405,'C111','AAA',15 UNION ALL
SELECT 201405,'D112','BBB',10
)
,m AS (
SELECT DISTINCT year_month FROM require
)
SELECT *
FROM store s
CROSS APPLY (
SELECT max(year_month)-s.year_month+1 inv_cyc --如果有跨年要转日期用DateDiff算
FROM ( --从s.year_month 开始,按月累积需求
SELECT m.year_month,
r.product_code,
SUM(r.inv_f) inv_f
FROM m
JOIN require r
ON r.year_month <= m.year_month
WHERE m.year_month >= s.year_month
AND r.year_month >= s.year_month
AND r.product_code = s.product_code
GROUP BY m.year_month, r.product_code
) tt
WHERE tt.inv_f <= s.inv
AND tt.product_code = s.product_code
) t
ORDER BY s.year_month,s.product_code
year_month product_code data_type inv inv_cyc
----------- ------------ --------- ----------- -----------
201401 C111 AAA 100 3
201401 D112 BBB 120 5
201402 C111 AAA 360 4
201402 C111 AAA 160 4
201402 D112 BBB 320 4
201402 D112 BBB 220 4
/****** Object: Table [dbo].[_temp_inventory] Script Date: 07/15/2014 19:57:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[_temp_inventory](
[year_month] [nvarchar](100) NULL,
[product_code] [nvarchar](100) NULL,
[data_type] [nvarchar](100) NULL,
[inv] [nvarchar](100) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201401', N'C111', N'AAA', N'100')
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201401', N'D112', N'BBB', N'120')
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201402', N'C111', N'AAA', N'160')
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201402', N'D112', N'BBB', N'220')
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201403', N'C111', N'AAA', N'360')
INSERT [dbo].[_temp_inventory] ([year_month], [product_code], [data_type], [inv]) VALUES (N'201403', N'D112', N'BBB', N'320')
/****** Object: Table [dbo].[_temp_Demand] Script Date: 07/15/2014 19:57:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[_temp_Demand](
[year_month] [nvarchar](100) NULL,
[product_code] [nvarchar](100) NULL,
[data_type] [nvarchar](100) NULL,
[inv_f] [nvarchar](100) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201401', N'C111', N'AAA', N'20')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201401', N'D112', N'BBB', N'30')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201402', N'C111', N'AAA', N'20')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201402', N'D112', N'BBB', N'60')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201403', N'C111', N'AAA', N'50')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201403', N'D112', N'BBB', N'20')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201404', N'C111', N'AAA', N'20')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201405', N'C111', N'AAA', N'15')
INSERT [dbo].[_temp_Demand] ([year_month], [product_code], [data_type], [inv_f]) VALUES (N'201405', N'D112', N'BBB', N'10')
/****** Script for SelectTopNRows command from SSMS ******/
SELECT *
FROM [dbo].[_temp_inventory]
ORDER BY product_code,
year_month
SELECT *
FROM _temp_Demand
ORDER BY product_code,
year_month;
WITH CTE([year_month], [year_month2], [product_code], [data_type], [inv_original], [inv], cnt)
AS (SELECT A.[year_month],
A.[year_month] AS [year_month2],
A.[product_code],
A.[data_type],
A.[inv] * 1 AS [inv_original],
A.[inv] * 1 - B.inv_f,
CASE WHEN a.[inv]*1-b.inv_f*1>0 THEN 1 ELSE 0 END --确认单前的库存是否满足需求,满足+1,不满足+0
FROM [dbo].[_temp_inventory] A
INNER JOIN [dbo].[_temp_Demand] B
ON a.[data_type] = b.[data_type] AND A.[product_code] = B.[product_code]
AND A.[year_month] = B.[year_month]
UNION ALL
SELECT b.[year_month],
a.[year_month] AS [year_month2],
b.[product_code],
b.[data_type],
b.[inv_original] * 1 AS [inv_original],
b.[inv] * 1 - a.inv_f * 1,--当前剩余库存
b.cnt + CASE WHEN b.[inv]*1-a.inv_f*1>0 THEN 1 ELSE 0 END
FROM [dbo].[_temp_Demand] A,
CTE B
WHERE a.[data_type] = b.[data_type] AND A.[product_code] = B.[product_code]
AND A.[year_month] = B.[year_month2] + 1 AND b.[inv] * 1 - a.inv_f * 1 > 0)
SELECT A.[year_month],
A.[product_code],
A.[data_type],
A.cnt
FROM cte A
INNER JOIN (SELECT [year_month],
[product_code],
[data_type],
MAX(cnt) AS CNT
FROM CTE
GROUP BY [year_month],
[product_code],
[data_type]) B
ON a.[data_type] = b.[data_type] AND A.[product_code] = B.[product_code] AND A.[year_month] = B.[year_month] AND a.cnt = b.cnt
ORDER BY 3, 1
先
select
year_month,product_code,data_type,
(select
sum(inv_f) as inv_f
from
库存需求表 where product_code=t.product_code and data_type=t.data_type and year_month<=t.year_month) as inv_f
from
库存需求表 as t
你看得到的结果 然后自己再去做吧 哥只能帮你到这了