在SQL Server中将三个数量的总和除以三行。 制作三行,如:
50
50
28
答案 0 :(得分:0)
好的,你还没有澄清你的需求,所以我在这里走出困境。我提供的不仅仅是一个答案,而是鼓励你澄清你的问题。附件可能就是你想要的......但如果没有,这似乎很有可能,它应该让你更好地了解你真正需要提出的问题:
WITH MySplit AS
(
SELECT
128 as CurrentValue,
50 as MaxPartition
UNION ALL
SELECT
CurrentValue - MaxPartition,
MaxPartition
FROM
MySplit
WHERE
CurrentValue - MaxPartition > 0
)
SELECT
CASE
WHEN MaxPartition < CurrentValue then MaxPartition
ELSE CurrentValue
END AS Partitition
FROM
MySplit
假设某个表中的值为128(或其他),并且您希望将包含128的行拆分为包含50,50和28的三行(通常为50的分区加上最后一个的余数) ):
DECLARE @rows_to_split AS TABLE (my_number INT)
INSERT INTO @rows_to_split VALUES (128),(204),(96)
--Everyone should have a "numbers" table, you may need additional rows.
--Search SO for how to generate them efficiently.
DECLARE @numbers AS TABLE (num int)
INSERT INTO @numbers VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
SELECT
r.my_number,
n.num,
CASE WHEN n.num* 50 > r.my_number THEN (r.my_number - (n.num-1)*50) ELSE 50 END AS split
FROM
@rows_to_split r
JOIN @numbers n ON
(n.num-1)*50 < r.my_number
ORDER BY
r.my_number,
n.num
答案 1 :(得分:0)
----我已经用另一种方式看了我的代码。假设我有两个数字来分割。我们可以不使用循环来实现它
DECLARE @COUNT INT; DECLARE @ITEMCOUNT INT;
DECLARE @ShipTable table(
ID INT IDENTITY(1,1),
WbDItemNo char(15),
Qty int
)
INSERT INTO @ShipTable
VALUES
(
'TEST ITEM',
59
),('TEST ITEM 1',225)
SELECT * FROM @ShipTable
SET @COUNT=(SELECT COUNT(*) FROM @ShipTable)
DECLARE @TOTALQTY INT
DECLARE @UPAMOUNT INT
DECLARE @DIVISIONALVAL INT
DECLARE @DIVIDEBY INT
DECLARE @ITEM VARCHAR(50)
SET @DIVIDEBY=50;
DECLARE @ITEMNO VARCHAR(50)
WHILE(@COUNT>0)
BEGIN
SET @TOTALQTY=(SELECT Qty FROM @ShipTable WHERE ID=@COUNT)
SET @ITEM=(SELECT WbDItemNo FROM @ShipTable WHERE ID=@COUNT)
SET @UPAMOUNT=@TOTALQTY%@DIVIDEBY
SELECT @ITEM,@UPAMOUNT
SET @DIVISIONALVAL=@TOTALQTY/@DIVIDEBY
SET @DIVISIONALVAL=FLOOR(@DIVISIONALVAL)
WHILE(@DIVISIONALVAL>0)
BEGIN
SELECT @ITEM,@DIVIDEBY
SET @DIVISIONALVAL=@DIVISIONALVAL-1
END
SET @COUNT=@COUNT-1
END