0

我在 sql server 表中有如下数据:

  Slno  Revenue     Data        Loading
   1    8989898     10/07/12    Before Load
   2    2124124     10/07/12    After Load

我想减去这两个值并将结果显示为百分比说“10%”

请帮助正确查询以计算以下内容

谢谢

4

2 回答 2

0

这可能有效

 Declare @Sample Table
 ( SNo int,
   revenue int,
   date datetime,
   Loading varchar(20)
 )

Insert into @Sample
values 
(1, 8989898,'10/07/12','Before Load'),
(2,2124124 ,'10/07/12','After Load')

 SELECT  
((cast(a.revenue as float) - cast(b.revenue as float))/cast(a.revenue as float))*100.0 AS     diff
FROM @Sample a
LEFT OUTER JOIN @Sample b
ON  b.Sno = (a.Sno +1)

如果收入列的数据类型是浮点数,则无需强制转换。编辑 :

 SELECT  
cast(((cast(f.revenue as float) - cast(f2.revenue as float))/cast(f.revenue as float))*100.0 as varchar(max))+'%'
 AS diff
FROM @Sample f
LEFT OUTER JOIN @Sample f2
ON  f2.Sno = (f.Sno +1)
于 2012-07-25T09:15:11.837 回答
0
select convert(varchar,((t.revenue - tt.revenue)*100)/(t.revenue)) +'%'   as value
from dbo.Table_1 t
inner join dbo.Table_1 tt on tt.data = t.data and t.loading = 'Before Load' and tt.loading = 'After Load'

这是假设在单个日期仅存在一个加载前(和加载后)

于 2012-07-25T09:16:14.117 回答