[英]Split a row into multiple rows based on a column value into table in SQL SERVER
[英]SQL Server : split row into multiple rows based on a column value
我有一個關於根據列值拆分行的問題
我的示例數據集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
我想將具有某些費用類型(例如汽車)的行拆分為兩行。 其他應保持為一排。
First row Price *70
Second Row Price *30
返回的數據集應該是
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
感謝您提前回答
我有一個關於根據列值拆分行的問題
我的示例數據集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
我想將那些具有某些費用類型(例如Car)的行拆分為兩行。 其他應保持為一行。
First row Price *70
Second Row Price *30
返回的數據集應為
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
預先感謝您的回答
我有一個關於根據列值拆分行的問題
我的示例數據集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
我想將那些具有某些費用類型(例如Car)的行拆分為兩行。 其他應保持為一行。
First row Price *70
Second Row Price *30
返回的數據集應為
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
預先感謝您的回答
我有一個關於根據列值拆分行的問題
我的示例數據集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
我想將那些具有某些費用類型(例如Car)的行拆分為兩行。 其他應保持為一行。
First row Price *70
Second Row Price *30
返回的數據集應為
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
預先感謝您的回答
我遇到了類似的需求,這是我的解決方案。
問題陳述:
我的組織正在從內部構建系統切換到第三方系統。 原始系統中的數值超過了目標系統可以處理的值大小。 第三方系統不允許我們增加字段大小,因此我們需要將數據拆分為不超過字段大小限制的值。
詳細信息:目標系統只能支持 10 億以下的值(可以包含負號)
例子:
DROP TABLE IF EXISTS #MyDemoData /* Generate some fake data for the demo */
SELECT item_no = 1, item_description = 'zero asset', amount = 0 INTO #MyDemoData
UNION SELECT item_no = 2, item_description = 'small asset', amount = 5100000
UNION SELECT item_no = 3, item_description = 'mid asset', amount = 510000000
UNION SELECT item_no = 4, item_description = 'large asset', amount = 5100000000
UNION SELECT item_no = 5, item_description = 'large debt', amount = -2999999999.99
SELECT * FROM #MyDemoData
DECLARE @limit_size INT = 1000000000
DROP TABLE IF EXISTS #groupings;
WITH
max_groups AS
(
SELECT max_groups=100
)
,groups AS
(
SELECT 1 AS [group]
UNION ALL
SELECT [group]+1
FROM groups
JOIN max_groups ON 1=1
WHERE [group]+1<=max_groups
)
,group_rows AS
(
SELECT 0 AS [row]
UNION ALL
SELECT [row]+1
FROM group_rows
JOIN max_groups ON 1=1
WHERE [row]+1<=max_groups
)
,groupings AS
(
SELECT [group],[row]
FROM group_rows
CROSS JOIN groups
WHERE [row] <= [group]
)
SELECT * INTO #groupings FROM groupings;
WITH /* Split out items that are under the limit and over the limit */
t1 AS /* Identify rows that are over the limit and by how many multiples over it is */
(
SELECT
item_no
, item_description
, amount
, over_limit = FLOOR(ABS(amount/@limit_size))
FROM #MyDemoData
)
SELECT /* select the items that are under the limit and do not need manipulated */
item_no
, item_description
, amount = CAST(amount AS DECIMAL(16,2))
FROM t1
WHERE ABS([amount]) < @limit_size
UNION ALL /* select the items that are over the limit, join on the groupings cte and calculate the split amounts */
SELECT
item_no
, item_description
, [Amount] = CAST(
CASE
WHEN row != 0 THEN (@limit_size-1) * ([amount]/ABS([amount]))
ELSE (ABS([amount]) - (t1.over_limit * @limit_size) + t1.over_limit) * ([amount]/ABS([amount]))
END AS DECIMAL(16,2))
FROM t1
JOIN #groupings bg ON t1.over_limit = bg.[group]
WHERE ABS([amount]) >= @limit_size
ORDER BY item_no
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.