Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 具有聚合函数的计算列_Sql Server - Fatal编程技术网

Sql server 具有聚合函数的计算列

Sql server 具有聚合函数的计算列,sql-server,Sql Server,我的表中有七列: 小时、重量、状态、总小时、总重量和百分比 total_weight = weight where status = 'X' total_hours = hours where status = 'X' percentage = total_hours/sum(weight) 总重量=重量,其中状态='X' 总时数=时数,其中状态='X' 百分比=总时数/总和(重量) 总和(权重)是一个聚合函数 我想指定生成三个计算列的公式。 我该怎么办?根据您所说的,您希望3列是整个表中数据的

我的表中有七列: 小时、重量、状态、总小时、总重量和百分比

total_weight = weight where status = 'X' total_hours = hours where status = 'X' percentage = total_hours/sum(weight) 总重量=重量,其中状态='X' 总时数=时数,其中状态='X' 百分比=总时数/总和(重量) 总和(权重)是一个聚合函数

我想指定生成三个计算列的公式。
我该怎么办?

根据您所说的,您希望3列是整个表中数据的聚合?SQL Server不允许这样做,因为聚合函数不能在计算列中使用。现在,您可以让用户定义的标量函数返回列中的值,但这无法持久化,在这种情况下,我看不出有什么价值。

到目前为止,您想出了什么样的SQL?你有什么问题吗?我们需要先看看你的作品,然后才能提供帮助。