产品数量扣除条件

时间:2017-07-19 15:13:14

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

我要求在条件下扣除产品数量。它似乎有点复杂,不知道如何使用SQL查询。以下是它的概念:产品在这里意味着原材料。出于生产目的,我们必须从库存中扣除原材料。几条规则可以遵循:

表格 - ProductEntry:

i)使用PO(采购订单)和供应商的发票编号购买产品。在这种情况下有一个条件。假设已经购买了100个产品ID为1001的产品,它分为以下两个部分:

Id - ProductId - PO - Invoice no - Quantity - Price - EntryDate

1st section: 1 - 1001 - PO-102 - Inv-122 - 20 - 200 - 2017-07-10 10:00:00

2nd section: 2 - 1001 - PO-102 - Inv-122 - 80 - 800 - 2017-07-10 11:00:00

             3 - 1002 - PO-102 - Inv-122 - 20 - 400 - 2017-07-10 10:00:00

这里开始游戏。在许多情况下,原材料或产品可能分为多个部分或完全一次,我的意思是总共100件。

ii)现在购买之后,它必须进入商店,还有另一个程序。每个购买的产品应分别收到IP(进口许可证)号码,如下所示:

表格 - IpEntry:

Id - ProductId - Invoice no - IP - AnotherProductId

1 - 1001 - Inv-122 - IP2244 - 2

2 - 1001 - Inv-122 - IP2244 - 2

3 - 1002 - Inv-122 - IP2244 - 4

iii)收到产品后,应将其用于生产目的,即消费。在消费中,应使用首次输入的产品或原材料。这意味着,如果必须扣除产品ID 1001,则应根据 EntryDate ' EntryDate '因为它已进入最低点。时间是同一天的10:00:00。因此,对于扣除或消费,应该遵循:

表 - 消费:

Id - 消费否 - AnotherProductId - 数量

1 - Con-122 - 2 - 10

3 - Con-122 - 4 - 10

所以最终输出如下:

Id - AnotherProductId - Stock - Quantity Used - Remaining Balance
1 - 2 - 10 - 10 - 100
2 - 4 - 10 - 10 - 200

我不在这里分享sql查询,因为使用返回以下内容的INNER JOINMIN函数应该不够准确和简单:

  Id - AnotherProductId - Stock - Quantity Used - Remaining Balance
   1 - 2 - 10 - 10 - 100
   2 - 2 - 10 - 10 - 100 //It returns **AnotherProductId or ProductId - 1001 or 2** twice as it should only return once
   3 - 4 - 10 - 10 - 200

我不知道如何处理上述情况,特别是同一产品,数量不同而且有点混淆。

以下是更好理解的脚本:

USE [Demo]
GO
/****** Object:  Table [dbo].[ProductEntry]    Script Date: 07/19/2017 20:37:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductEntry](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [PO] [nvarchar](60) NULL,
    [Invoice No] [nvarchar](60) NULL,
    [Quantity] [float] NULL,
    [Price] [float] NULL,
    [EntryDate] [datetime] NULL,
 CONSTRAINT [PK_ProductEntry] 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
SET IDENTITY_INSERT [dbo].[ProductEntry] ON
INSERT [dbo].[ProductEntry] ([Id], [ProductId], [PO], [Invoice No], [Quantity], [Price], [EntryDate]) VALUES (1, 1001, N'PO-102', N'Inv-122', 20, 200, CAST(0x0000A7AC00A4CB80 AS DateTime))
INSERT [dbo].[ProductEntry] ([Id], [ProductId], [PO], [Invoice No], [Quantity], [Price], [EntryDate]) VALUES (2, 1001, N'PO-102', N'Inv-122', 80, 800, CAST(0x0000A7AC00B54640 AS DateTime))
INSERT [dbo].[ProductEntry] ([Id], [ProductId], [PO], [Invoice No], [Quantity], [Price], [EntryDate]) VALUES (3, 1002, N'PO-102', N'Inv-122', 20, 400, CAST(0x0000A7AC00A4CB80 AS DateTime))
SET IDENTITY_INSERT [dbo].[ProductEntry] OFF
/****** Object:  Table [dbo].[IpEntry]    Script Date: 07/19/2017 20:37:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[IpEntry](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [Invoice No] [nvarchar](60) NULL,
    [IP] [nvarchar](60) NULL,
    [AnotherProductId] [int] NULL,
 CONSTRAINT [PK_IpEntry] 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
SET IDENTITY_INSERT [dbo].[IpEntry] ON
INSERT [dbo].[IpEntry] ([Id], [ProductId], [Invoice No], [IP], [AnotherProductId]) VALUES (1, 1001, N'Inv-122', N'IP2244', 2)
INSERT [dbo].[IpEntry] ([Id], [ProductId], [Invoice No], [IP], [AnotherProductId]) VALUES (2, 1001, N'Inv-122', N'IP2244', 2)
INSERT [dbo].[IpEntry] ([Id], [ProductId], [Invoice No], [IP], [AnotherProductId]) VALUES (3, 1002, N'Inv-122', N'IP2244', 4)
SET IDENTITY_INSERT [dbo].[IpEntry] OFF
/****** Object:  Table [dbo].[Consumption]    Script Date: 07/19/2017 20:37:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Consumption](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Consumption no] [nvarchar](40) NULL,
    [AnotherProductId] [int] NULL,
    [Quantity] [float] NULL,
 CONSTRAINT [PK_Consumption] 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
SET IDENTITY_INSERT [dbo].[Consumption] ON
INSERT [dbo].[Consumption] ([Id], [Consumption no], [AnotherProductId], [Quantity]) VALUES (1, N'Con-122 ', 2, 10)
INSERT [dbo].[Consumption] ([Id], [Consumption no], [AnotherProductId], [Quantity]) VALUES (2, N'Con-122 ', 4, 10)
SET IDENTITY_INSERT [dbo].[Consumption] OFF

2 个答案:

答案 0 :(得分:1)

这应该给你预期的结果。请试试。

{{1}}

答案 1 :(得分:1)

预计这将涵盖所有情景。

SELECT DISTINCT P.ProductID,P.Quantity,-1 Flag,C.[Quantity] Balance
INTO #TMP 
FROM [ProductEntry] P
INNER JOIN [IpEntry] I ON I.ProductID=P.ProductId 
        INNER JOIN (SELECT [AnotherProductId],SUM([Quantity]) [Quantity] FROM [Consumption] GROUP BY [AnotherProductId])C ON C.AnotherProductId=I.AnotherProductId

DECLARE @Counter INT=1
WHILE((SELECT TOP 1 1 FROM #TMP WHERE Flag=-1 )=1)
BEGIN
    UPDATE T SET T.Balance = T.Balance-T.Quantity,
                 T.Quantity = CASE WHEN T.Quantity-T.Balance>=0 THEN T.Quantity-T.Balance ELSE 0 END,
                 T.Flag = CASE WHEN T.Quantity-T.Balance>=0 THEN 0 ELSE 1 END

    FROM (SELECT  ProductId,Quantity,row_number() over (partition by ProductId order by Quantity)RID FROM [ProductEntry])P
            INNER JOIN [IpEntry] I ON I.ProductID=P.ProductId and P.RID=@Counter
            INNER JOIN (SELECT ProductId,Quantity,Flag,Balance,row_number() over (partition by ProductId order by Quantity)RID FROM #TMP ) T ON T.ProductID=P.ProductID and T.RID=@Counter
            INNER JOIN (SELECT [AnotherProductId],SUM([Quantity]) [Quantity] FROM [Consumption] GROUP BY [AnotherProductId])C ON C.AnotherProductId=I.AnotherProductId

    UPDATE T1 SET Balance=T2.Balance
    FROM #TMP T1 INNER JOIN #TMP T2 ON T1.ProductId=T2.ProductId
    WHERE T2.Flag IN (0,1)
    UPDATE T1 SET Flag= (SELECT T2.Flag FROM #TMP T2 WHERE T1.ProductId=T2.ProductId AND T2.Flag=0)
    FROM #TMP T1
    WHERE Flag=0

    SET @Counter=@Counter+1
    SELECT * FROM #TMP
END     
SELECT ProductId,Quantity FROM #TMP --You can add more details by joining with other tables as per your requirement
drop table #TMP