代码之家  ›  专栏  ›  技术社区  ›  Pradap Pandian

如何在SQL Server 2008中从Avg中查找Max

  •  1
  • Pradap Pandian  · 技术社区  · 8 年前
    select
        max(total_price_new) 
    from
        (select
             avg(Total_Price) as total_price_new, 
             Document_Ref  
         from
             TBL_Sales
         group by
             Document_Ref)
    

    这在SQLite中有效,但在SQL Server 2008中无效。

    enter image description here

    有人知道这个的替代方案吗?

    4 回复  |  直到 8 年前
        1
  •  4
  •   ScaisEdge    8 年前

    你需要表名别名如Tname

    select max(total_price_new) 
    from  ( 
      select avg(Total_Price) as total_price_new, Document_Ref 
      from  TBL_Sales
    group by Document_Ref
    )  Tname 
    
        2
  •  3
  •   Barbaros Özhan    8 年前

    别名 ( q

    select max(q.total_price_new) 
      from  ( select avg(Total_Price) as total_price_new, Document_Ref 
                from TBL_Sales
               group by Document_Ref) as q;
    

    This 参考资料可能会有帮助。

        3
  •  2
  •   Zaynul Abadin Tuhin    8 年前

    select
        max(total_price_new) 
    from
        (select
             avg(Total_Price) as total_price_new, 
             Document_Ref  
         from
             TBL_Sales
         group by
             Document_Ref) as T
    
        4
  •  1
  •   Gordon Linoff    8 年前

    此操作不需要子查询:

    select top (1) avg(Total_Price) as total_price_new
    from TBL_Sales
    group by Document_Ref
    order by total_price_new desc;
    

    Document_Ref 平均值最高。