简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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