我有一個ms-sql表格,它看起來像這樣(weight = kg)。計算給定的一組項目(權重)所需的盒子數量
我希望能夠計算每需要一個給定的標識符盒箱數和重量。一個盒子最多可以容納30公斤。該標識符的所有項目可以在一個框中混合。我僅限於sql(2008),但一切都可以使用(CTE,函數,StoredProcs等)。我嘗試了不同的方法(CTE,函數),但我無法獲得正確的結果。任何形式的幫助表示讚賞。
預期輸出選擇時
標識符100001:
選擇標識符100002時:
選擇時標識符100003:
選擇標識符100004時:
UPDATE
試樣臺
CREATE TABLE [dbo].[tblTest](
[position] [int] NOT NULL,
[item] [varchar](31) NOT NULL,
[quantity] [money] NOT NULL,
[weight] [money] NOT NULL,
[identifier] [varchar](50) NOT NULL,
CONSTRAINT [PK_tblTest] PRIMARY KEY CLUSTERED
(
[position] ASC,
[identifier] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
個
樣本數據
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000001', 4.0000, 10.0000, N'100001')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000003', 3.0000, 15.0000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000006', 7.0000, 25.0000, N'100003')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000007', 1.0000, 1.5000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0023021', 2.0000, 14.5000, N'100005')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000002', 1.0000, 15.0000, N'100001')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000004', 1.0000, 5.0000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000008', 1.0000, 2.5000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0023022', 3.0000, 17.5000, N'100005')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (3, N'0000005', 3.0000, 2.5000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (3, N'0000009', 3.0000, 6.0000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (4, N'0000010', 1.0000, 1.0000, N'100004')
這是(幾乎是字面上)垃圾包裝問題。我沒有一個有效的SQL精確解決方案。 –
有關裝箱問題的更多信息... https://en.wikipedia.org/wiki/Bin_packing_problem –
謝謝您的提示與裝箱。目前閱讀https://gertjans.home.xs4all.nl/sql/binpacking/intro.html – Belial09