Oracle11g 如何使用count(*)获得结果的总数?

Oracle11g 如何使用count(*)获得结果的总数?,oracle11g,Oracle11g,我需要得到每个人的结果总数,但我得到 我的问题 select t.fecha_hora_timbre,e.nombre,e.apellido,d.descripcion as departamento_trabaja, t.fecha,count(*) from fulltime.timbre t, fulltime.empleado e, fulltime.departamento d where d.depa_id=e.depa_id and t.codigo_empleado=e.cod

我需要得到每个人的结果总数,但我得到

我的问题

select t.fecha_hora_timbre,e.nombre,e.apellido,d.descripcion as departamento_trabaja, t.fecha,count(*) from fulltime.timbre t, fulltime.empleado e, fulltime.departamento d where d.depa_id=e.depa_id and t.codigo_empleado=e.codigo_empleado and trunc(t.fecha) between trunc(to_date('15/02/2017','dd/mm/yyyy')) and trunc(to_date('14/03/2017','dd/mm/yyyy')) group by t.fecha_hora_timbre,e.nombre,e.apellido,d.descripcion, t.fecha
您应该只按实际要计数的非聚合列进行选择和分组。目前,您正在将
fecha\u hora\u timbre
fecha
列包含在每一行中,因此您要计算这些列的唯一组合以及实际要计算的名称/部门信息

select e.nombre, e.apellido, d.descripcion as departamento_trabaja,
  count(*) a veces_marcadas
from fulltime.timbre t
join fulltime.empleado e on t.codigo_empleado=e.codigo_empleado
join fulltime.departamento d on d.depa_id=e.depa_id
where t.fecha >= to_date('15/02/2017','dd/mm/yyyy')
and t.fecha < to_date('15/03/2017','dd/mm/yyyy')
group by e.nombre, e.apellido, d.descripcion
选择e.nombre、e.apellido、d.description作为departmento_trabaja,
计数(*)a veces_marcadas
从全职开始
在t.codigo_empleado=e.codigo_empleado上加入fulltime.empleado
加入fulltime.departamento d on d.depa_id=e.depa_id
其中t.fecha>=截止日期('2017年2月15日','dd/mm/yyyy')
和t.fecha<截至日期('2017年3月15日','dd/mm/yyyy'))
按e.nombre、e.apellido、d.Description分组
我已经删除了额外的列。请注意,它们已从select列表和GROUPBY子句中删除。如果选择列表中的非聚合列不在group by中,则会出现ORA-00937错误;但是,如果在“分组依据”中有一列不在“选择”列表中,则它仍将按该列分组,即使您看不到该列,也无法获得预期的结果


我还将老式的连接语法改为现代语法。我更改了日期比较;首先,因为做
trunc()
作为
trunc的一部分(截止日期('15/02/2017','dd/mm/yyyy')
是毫无意义的-你已经知道时间部分是午夜,所以trunc没有实现任何目标。但大多数情况下,如果fecha上有索引,则可以使用该索引。如果执行
trunc(f.techa)
操作,则必须截断每个列值的值,这将停止使用索引(除非有基于函数的索引)。由于
介于
之间,使用
=
可以选择(并按)许多列;您将看到所有这些的每个独特组合的计数。您是否希望看到更少的列(没有
fecha\u hora\u音色
fecha
可能)以及剩余列的计数;或者,您想保留所有这些列,并计算每个人的总数;“Maria Tarcila”的三行都显示出来了,但是每一行都附加了一个“3”的计数?如果你将预期结果包括在内,我们会更好地了解你真正想要实现的目标。嗨@亚历克斯·普尔谢谢!!嗯,我需要保留每个人的总数量,例如:请编辑您的问题以显示文本,而不是图像。您当前正在选择和分组更多的列,这是为什么?删除不希望分组的列(从查询的两个部分中)。。。
select e.nombre,e.apellido,d.descripcion as departamento_trabaja,COUNT(*)
from fulltime.timbre t, fulltime.empleado e, fulltime.departamento d
where d.depa_id=e.depa_id and t.codigo_empleado=e.codigo_empleado and
            trunc(t.fecha) between trunc(to_date('15/02/2017','dd/mm/yyyy')) and trunc(to_date('14/03/2017','dd/mm/yyyy'))

group by t.fecha_hora_timbre,e.nombre,e.apellido,d.descripcion, t.fecha
select e.nombre, e.apellido, d.descripcion as departamento_trabaja,
  count(*) a veces_marcadas
from fulltime.timbre t
join fulltime.empleado e on t.codigo_empleado=e.codigo_empleado
join fulltime.departamento d on d.depa_id=e.depa_id
where t.fecha >= to_date('15/02/2017','dd/mm/yyyy')
and t.fecha < to_date('15/03/2017','dd/mm/yyyy')
group by e.nombre, e.apellido, d.descripcion