SQL 服务器:有没有办法从聚合函数中 select 别名列?

SQL Server: Is there a way to select alias column from aggregate function?

所以我通过执行聚合函数得到了这些列: Table

SELECT MachineName,

       (100*((1)
       -((
       sum(CASE p1.CounterName
             WHEN 'Available Bytes' THEN
               p2.CounterValue
             ELSE
               0
           END)
       /NULLIF(
       (sum(CASE p1.CounterName
              WHEN 'Available Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)
        +
        sum(CASE p1.CounterName
              WHEN 'Committed Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)
        +
        sum(CASE p1.CounterName
              WHEN 'Modified Page List Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)),0) ))))  As Memory

我只想选择 Memory > 75,但我对 SQL 和聚合函数不够熟悉,所以我不确定这是否可行。

您可以尝试使用如下子查询 -

select * from 
(
SELECT MachineName,

       (100*((1)
       -((
       sum(CASE p1.CounterName
             WHEN 'Available Bytes' THEN
               p2.CounterValue
             ELSE
               0
           END)
       /NULLIF(
       (sum(CASE p1.CounterName
              WHEN 'Available Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)
        +
        sum(CASE p1.CounterName
              WHEN 'Committed Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)
        +
        sum(CASE p1.CounterName
              WHEN 'Modified Page List Bytes' THEN
                p2.CounterValue
              ELSE
                0
            END)),0) ))))  As Memory
)A where memory>75