【问题标题】:SQL Server: Is there a way to select alias column from aggregate function?SQL Server:有没有办法从聚合函数中选择别名列?
【发布时间】:2019-05-30 16:35:26
【问题描述】:

所以我有这些列来自执行聚合函数: 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 和聚合函数不够熟悉,所以我不确定这是否可能。

【问题讨论】:

    标签: sql function aggregate alias


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢!做到了。但是我有一个后续问题,如果我还想选择出现 5 次的 MachineName 怎么办。我尝试过 Where Memory >75 and Being Count(MachineName)> 0 但它不起作用。我认为它不起作用,因为 MachinName 是一个 varchar 但我也不想列出所有机器名称并这样做: Where Memory > 75 and count(Machine Name = 'Machine 1') > 5因为我在 MachineName 列下有 30 多个机器名称。
    • @123testing123,再发个问题就更好了
    • 好的就行。谢谢你。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多