[英]Distribute one row of data to two rows in SQL
I have the table below. 我有下表。 Is there a good way to regroup them such that group 'a+b' gets double counted and added to group 'a' and 'b'
有没有一种很好的方法来重新组合它们,以便对组“ a + b”进行重复计数并添加到组“ a”和“ b”中
Group Amount
a 100
b 200
c 300
a+b 400
The result would be: 结果将是:
Group Amount
a 500
b 600
c 300
This is hideous, but you could do a subquery for matching groups. 这很可怕,但是您可以对匹配的组进行子查询。
SELECT GroupId,
Amount + ISNULL(
(SELECT SUM(Amount) FROM MyGroups t2
WHERE t2.GroupId <> t1.GroupId
AND t2.GroupId LIKE '%' + t1.GroupId + '%'), 0)
FROM MyGroups t1
WHERE t1.GroupId NOT LIKE '%+%'
http://sqlfiddle.com/#!3/50214/8 http://sqlfiddle.com/#!3/50214/8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.