Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql嵌套聚合函数_Sql_Postgresql - Fatal编程技术网

Postgresql嵌套聚合函数

Postgresql嵌套聚合函数,sql,postgresql,Sql,Postgresql,我想找到当月休假次数最多的员工 我从以下问题开始: select MAX(TotalLeaves) as HighestLeaves FROM (SELECT emp_id, count(adate) as TotalLeaves from attendance group by emp_id) AS HIGHEST; 但是我在显示员工id和仅获取当月结果方面遇到了问题。请帮助我。如果您只想在当前查询中显示相应的员工id,您可以对结果进行排序并获得前1行,并且您需

我想找到当月休假次数最多的员工

我从以下问题开始:

select MAX(TotalLeaves) as HighestLeaves 
FROM (SELECT emp_id, count(adate) as TotalLeaves 
      from attendance 
      group by emp_id) AS HIGHEST;

但是我在显示员工id和仅获取当月结果方面遇到了问题。请帮助我。

如果您只想在当前查询中显示相应的员工id,您可以对结果进行排序并获得前1行,并且您需要在组之前筛选数据以仅获得当前月份:

select
    emp_id, TotalLeaves
from (
    select emp_id, count(adate) as TotalLeaves 
    from attendance
    where adate >= date_trunc('month', current_date)
    group by emp_id
) as highest
order by TotalLeaves desc
limit 1;
实际上,这里根本不需要使用子查询:

select emp_id, count(adate) as TotalLeaves 
from attendance
where adate >= date_trunc('month', current_date)
group by emp_id
order by TotalLeaves desc
limit 1;

谢谢您的回答。这真的很有帮助。我还有另一个问题。我有两个表:销售(发票id、客户id、销售日期、总金额)和发票(发票id、数量、价格、产品id、总金额)。我怎样才能找到上周没有售出的产品???@user2944346我想最好再问一个问题
SELECT emp_id, count(adate) as TotalLeaves 
  from attendance
  where adata > date_trunc('month', NOW())
  group by emp_id
  order by 2 desc limit 1