0

我想要oracle中表的列值和相同的列数。

以下是查询:

select username,
       count(username) 
  from ind_emp_leaves 
 where  status != 'Approved' 
   and  (leavefromdate BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
                           AND to_date('11-09-2013','dd/MM/yyyy')) OR
        (LEAVETODATE  BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
                          AND to_date('11-09-2013','dd/MM/yyyy')) 

上面的查询显示错误:

SQL 错误:ORA-00937:不是单组组函数

没有用户名字段,它显示计数值,但我也想要用户名值和用户名计数。

4

3 回答 3

1

假设您想要每个用户的计数,这应该给出正确的结果:

select username, count(*) from ind_emp_leaves 
where  (status != 'Approved') 
  and  ((leavefromdate BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
     AND to_date('11-09-2013','dd/MM/yyyy')) 
  OR (LEAVETODATE  BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
     AND to_date('11-09-2013','dd/MM/yyyy')))
group by username

请注意,我在 leavedate 部分周围放置了额外的括号 - 我假设您希望检查两个间隔的“status!= 'Approved'”部分,而不仅仅是第一个。

于 2013-09-06T06:29:18.380 回答
0

嘿,如果您使用的是聚合函数,比如!

AVG
COLLECT
CORR
CORR_*
COUNT
COVAR_POP
COVAR_SAMP
CUME_DIST
DENSE_RANK
FIRST
GROUP_ID
GROUPING
GROUPING_ID
LAST

您应该按功能使用分组!

于 2013-09-06T06:58:37.540 回答
0

您可以使用以下代码获得所需的结果..

  select username, count(*) from ind_emp_leaves 
     where  status != 'Approved')
     and  ((leavefromdate BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
     AND to_date('11-09-2013','dd/MM/yyyy')) 
     OR (LEAVETODATE  BETWEEN to_date('08-09-2013','dd/MM/yyyy') 
     AND to_date('11-09-2013','dd/MM/yyyy')))
     group by username
于 2013-09-06T06:40:41.127 回答