我怎样才能添加两行 [英] how can i add two rows in

查看:67
本文介绍了我怎样才能添加两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这个



姓名id工资
raj 1 90 90
ram 2 23 113
jeet 3 15 128
neelu 4 20 148





在ram 2中获取余额我SUM Balance(90 )ram 1与Total(23)ram 2得到平衡(113)ram 2.通过添加前一行的Balance

解决方案

看起来你想要运行总计,这里似乎有几个解决方案 -

[ ^ ]


您好试试这个代码块,



  CREATE   TABLE  YourTable(ID  INT ,name  varchar  20 ),salary  money 

INSERT INTO YourTable(ID,名称,工资)
VALUES
1 ' raj' 90 ),
2 ' ram' 23 ),
3 ' jeet' 15 ),
4 ' neelu' 20

; WITH RecursiveCTE AS

SELECT TOP 1 ID,名称,工资,薪水 FROM YourTable ORDER BY ID asc
UNION ALL
SELECT YourTable.ID,YourTable。 name,YourTable.salary,Total + YourTable.salary FROM
RecursiveCTE JOIN YourTable ON YourTable.ID = RecursiveCTE.ID + 1


SELECT * FROM RecursiveCTE OPTION (Maxrecursion 0





快乐编码......


 选择 a.Id,a.Name,sum(b.salary) as  salary 
from tablename a cross join tablename b
其中 b.Id< = a.Id
group by a.Id,a .NAME


I want this

name  id salary 
raj   1  90   90
ram   2  23  113
jeet  3  15  128
neelu 4  20  148



In ram 2 to get the balance I SUM Balance(90) of raj 1 with Total(23) of ram 2 to get Balance (113) of ram 2. Same thing is done for the next row by adding with Balance of previous row

解决方案

Looks like you want running totals, and there seems to be several solutions mentioned here -
[^]


Hi try this code block,

CREATE TABLE YourTable (ID int,name varchar(20),salary money)

INSERT INTO YourTable (ID,name,salary) 
VALUES 
(1, 'raj',90),
(2,'ram', 23),
(3,'jeet',15 ),
(4,'neelu',20)

 ;WITH RecursiveCTE AS 
(
  SELECT TOP 1 ID, name,salary, salary as Total FROM YourTable ORDER BY ID asc
  UNION ALL
  SELECT YourTable.ID,YourTable.name,YourTable.salary,Total+YourTable.salary FROM 
  RecursiveCTE JOIN YourTable ON YourTable.ID = RecursiveCTE.ID + 1
)
 
 SELECT * FROM RecursiveCTE OPTION (Maxrecursion 0)



Happy coding...


select a.Id, a.Name, sum(b.salary) as salary
from tablename  a cross join tablename b
where b.Id <= a.Id
group by a.Id, a.Name


这篇关于我怎样才能添加两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆