简体   繁体   English

SQL Server:是否可以从聚合函数中选择别名列?

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

So I have these columns from performing an 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

I only want Memory > 75 that needs to be selected but I'm not familiar enough with SQL and aggregate function so I'm not sure if this is possible. 我只希望选择需要> 75的“内存”,但是我对SQL和聚合函数不够熟悉,因此我不确定是否可行。

You can try using subquery like below - 您可以尝试使用如下子查询-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM