0

我写了一个像这样的子查询:

(
  SELECT SUM(X.kMax) 
        FROM (
            SELECT MAX(Val) AS kMax 
            FROM [Consumers] upc 
            GROUP BY upc.[Profile]
        ) X
)

它获取每个配置文件的最大值,然后将所有内容相加以返回一个值。例如 99

我将如何将此值应用于外部查询中的每一行?

Table

ID   Maxed
1      99
2      99
3      99
4      99
5      99
4

2 回答 2

1

您需要像以下那样编写查询。

select 
<columns>,
(
  SELECT SUM(X.kMax) 
        FROM (
            SELECT MAX(Val) AS kMax 
            FROM [Consumers] upc 
            WHERE UPC.ID=UT.ID
            GROUP BY upc.[Profile]
        ) X
)
from yourTable ut
于 2021-10-07T09:18:30.747 回答
1

您可以使用或应用一个值CROSS APPLYOUTER APPLY

select 
  <columns>,
  X.kMax
from yourTable T
CROSS APPLY (
  SELECT SUM(X.kMax) AS kMax 
        FROM (
            SELECT MAX(Val) AS kMax 
            FROM [Consumers] upc 
            WHERE UPC.ID=UT.ID
            GROUP BY upc.[Profile]
        ) X
) X
于 2021-10-07T10:28:17.303 回答