提取SQL中的权重和倍数

问题描述 投票:-4回答:1

我有这样的数据:80X71X120CM @ 95KG,80X71X120CM @ 96KG X 2,80X71X107CM @ 76KG X 5。

如何获得重量的总和(95 + 96 * 2 + 76 * 5)?

sql ssrs-2012
1个回答
0
投票
首先,我不认为这是存储数据的好方法,就像评论所说的,目前尚不清楚。它是一个包含所有3个帖子的字符串还是3行?是否总是用@分隔它?我把它当作3行,所以这是您可以做的,尽管非常笨拙...

` declare @table as table(stringdata nvarchar(100)) insert into @table values ('80X71X120CM@95KG') insert into @table values ('80X71X120CM@96KG X 2') insert into @table values ('80X71X107CM@76KG X 5') ;with cte as ( select substring(stringdata,charindex('@', stringdata) +1,len(stringdata) -charindex('@', stringdata)) as WeightData from @table ), cte2 as ( select left(WeightData, charindex('KG', WeightData)-1) as weight, case when charindex('X', WeightData) > 0 then convert(integer, (substring(WeightData,charindex('X', WeightData) +1,len(WeightData) -charindex('X', WeightData)))) else 1 end as NumberOfPersons, case when charindex('X', WeightData) > 0 then convert(integer, (substring(WeightData,charindex('X', WeightData) +1,len(WeightData) -charindex('X', WeightData)))) * convert(integer, left(WeightData, charindex('KG', WeightData)-1) ) else convert(integer, left(WeightData, charindex('KG', WeightData)-1) ) end as RowWeight from cte ) select sum(RowWeight) as TotalWeight from cte2 `

© www.soinside.com 2019 - 2024. All rights reserved.