mssql:为所有行添加具有相同值的列以搜索结果

时间:2019-02-15 08:20:49

标签: sql sql-server database

我有我的查询:

    SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER 
    (ORDER BY [Shipment Date]) as [Total Running Costs]
    FROM...

这使我得到3列:

Shipment Date | Running Costs | Total Running Costs

我想在此查询中添加第四列,该列的所有行都具有相同的值,并且行数与原始查询结果相同。

我知道您可以在搜索结果中添加例如“ 999”作为Something,但是我如何对另一列的总和执行相同操作(例如:假设另一张表中某列的总和为1500,我想在第四列的所有行中都设置1500。类似于select sum(column_name)吗?

数据库引擎是MSSQL。

1 个答案:

答案 0 :(得分:1)

您可以使用嵌套查询

SELECT [Shipment Date], [Amount] as [Running Costs], [Total Running Costs], SUM([Total Running Costs] OVER ())  
FROM  
(  
 SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER   
 (ORDER BY [Shipment Date]) as [Total Running Costs]  
 FROM...  
)  

嵌套窗口功能也应该起作用

SUM(SUM([Running costs]) OVER (ORDER BY [Shipment Date])) OVER ()