MS-SQL:在生成的列中添加列的值 [英] MS-SQL: Add the value of a column in a generated column
本文介绍了MS-SQL:在生成的列中添加列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含ID和值的表.
ID值
2 100
5 250
8150
12200
现在我想要一个查询,该查询对值"列进行降序排序.
SELECT ID,值
FROM 值
ORDER BY 值 DESC
但是它必须具有另一列(总计),其中每条记录都将值添加到该列中,因此结果将是这样:
ID值总计
5250250
12200400(250 + 200)
8150550(400 + 150)
2100650(550 + 100)
我该如何实现?
解决方案
在这里尝试? 在SQL Server中计算简单运行总计 [ ^ ]
在这种情况下的特定查询例如可以是:
SELECT A.ID, 一个值 ( SELECT SUM(值) FROM 值 B 位置 B.Id< = A.Id) AS 总计 FROM 值 A 订单 BY A.Value DESC
使用游标或编写一个对值进行计数的C#应用程序,对此没有SELECT语句.
Hi,
I have a table which hold a ID and a Value.
ID Value
2 100
5 250
8 150
12 200
Now I want a query which orders the Value column descending.
SELECT ID, Value
FROM Values
ORDER BY Value DESC
But it has to have another column(Total) where the value is added with each record, so the result will be this:
ID Value Total
5 250 250
12 200 400 (250 + 200)
8 150 550 (400 + 150)
2 100 650 (550 + 100)
How can I achive this?
解决方案
try here? Calculating simple running totals in SQL Server[^]
The specific query in this case could be for example something like:
SELECT A.ID, A.Value (SELECT SUM(Value) FROM Values B WHERE B.Id <= A.Id) AS Total FROM Values A ORDER BY A.Value DESC
Use a cursor or write a C# app that counts the values, there''s no SELECT statement for this.
这篇关于MS-SQL:在生成的列中添加列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文