【问题标题】:Possible positions and executing order about aggregation functions in select statement in sql?sql中select语句中聚合函数的可能位置和执行顺序?
【发布时间】:2014-10-31 06:37:49
【问题描述】:

我在哪里可以将聚合函数放在一个 SQL 查询中(在 select 子句、having 子句或其他子句中)。而在select语句的逻辑处理顺序中聚合函数的执行顺序是什么?

那里的帖子“Order Of Execution of the SQL query”没有涵盖函数的执行顺序。

【问题讨论】:

    标签: mysql sql-server sql-function


    【解决方案1】:

    1) 子查询可以放在sql语句的Where、from和middle之后。

    //After Where   
     select * from orders where order_id in (select Oreder_id from OrderConfirmed)
    // After From
     Select * from (select * from table) a
    //Middle of sql Statement
     select id,(select Id from table1) form table2
    
    
     2) You Can put Aggregation function in both select clause and having clause.
    
        //In select
        select count(employee) from employees group by employee_dept
        //In having
        select count(employee) from employees group by employee_dept having(max(salary)>2000) 
    
    3) order of execution of sql statement
    
      1)From 
    
      2)Where
    
      3)Group By
    
      4)Having
    
      5)aggregate Function
    
      6)Select 
    

    【讨论】:

    • link的回答看,HavingGroup By的顺序不同。
    • 在 group by 之前执行。
    • 你确定吗?根据我的经验,我认为group byhaving 之前执行。
    • 抱歉 group by 在有之前执行。
    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2018-02-18
    • 1970-01-01
    • 2016-12-19
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多