[英]SQL Server : split row into multiple rows based on a column value
I have a question regarding splitting rows based on column value我有一个关于根据列值拆分行的问题
My example data set is :我的示例数据集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
I want to split rows those have some Expense Types such as Car into two rows .我想将具有某些费用类型(例如汽车)的行拆分为两行。 Others should remain as one row.其他应保持为一排。
First row Price *70
Second Row Price *30
Returned dataset should be返回的数据集应该是
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
Thanks for your answers in advance感谢您提前回答
I have a question regarding splitting rows based on column value我有一个关于根据列值拆分行的问题
My example data set is :我的示例数据集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
I want to split rows those have some Expense Types such as Car into two rows .我想将那些具有某些费用类型(例如Car)的行拆分为两行。 Others should remain as one row.其他应保持为一行。
First row Price *70
Second Row Price *30
Returned dataset should be返回的数据集应为
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
Thanks for your answers in advance预先感谢您的回答
I have a question regarding splitting rows based on column value我有一个关于根据列值拆分行的问题
My example data set is :我的示例数据集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
I want to split rows those have some Expense Types such as Car into two rows .我想将那些具有某些费用类型(例如Car)的行拆分为两行。 Others should remain as one row.其他应保持为一行。
First row Price *70
Second Row Price *30
Returned dataset should be返回的数据集应为
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
Thanks for your answers in advance预先感谢您的回答
I have a question regarding splitting rows based on column value我有一个关于根据列值拆分行的问题
My example data set is :我的示例数据集是:
id ExpenseType Price
------------------------
1 Car 100
2 Hotel 50
I want to split rows those have some Expense Types such as Car into two rows .我想将那些具有某些费用类型(例如Car)的行拆分为两行。 Others should remain as one row.其他应保持为一行。
First row Price *70
Second Row Price *30
Returned dataset should be返回的数据集应为
id ExpenseType Price
-----------------------
1 Car 70
1 Car 30
2 Hotel 50
Thanks for your answers in advance预先感谢您的回答
I ran into a similar need, here is my solution.我遇到了类似的需求,这是我的解决方案。
Problem statement:问题陈述:
My organization is switching from an in-house build system to a third-party system.我的组织正在从内部构建系统切换到第三方系统。 Numerical values in the original system surpassed the value size that the destination system could handle.原始系统中的数值超过了目标系统可以处理的值大小。 The third-party system will not allow us to increase the field size, as a result we need to split the data up into values that do not surpass the field size limit.第三方系统不允许我们增加字段大小,因此我们需要将数据拆分为不超过字段大小限制的值。
Details: Destination system can only support values under 1 billion (can include a negative sign)详细信息:目标系统只能支持 10 亿以下的值(可以包含负号)
Example:例子:
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.