我必须创建一个sql查询,显示去年每个月进行预订的客户的所有信息,我考虑使用按月分组然后计算组数。
像这样:(我不知道如何计算群组的数量)
Select *
From Cliente
inner join Persona
on Cliente.CEDULA = Persona.CEDULA
inner join Reserva
on Cliente.cedula = Reserva.CEDULA
Group BY DATEPART(MONTH, Reserva.FECHAINICIO);
答案 0 :(得分:0)
您是否搜索count()
?
Select DATEPART(MONTH, Reserva.FECHAINICIO), count(*)
From Cliente
inner join Persona
on Cliente.CEDULA = Persona.CEDULA
inner join Reserva
on Cliente.cedula = Reserva.CEDULA
Group BY DATEPART(MONTH, Reserva.FECHAINICIO);
答案 1 :(得分:0)
尝试使用bellow查询,为client.id_column,date_column
输入相关的列名Select Month, COUNT(<<client.id_column>>) TotalCount
From Cliente
inner join Persona
on Cliente.CEDULA = Persona.CEDULA
inner join Reserva
on Cliente.cedula = Reserva.CEDULA
where Reserva.<<date_column>> >= '2017-01-01'
AND Reserva.<<date_column>> <= '2017-12-31'
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Reserva.<<date_column>>), 0), as Month;
答案 2 :(得分:0)
假设cedula
标识了客户端,则不需要所有连接。然后关键是日期算术部分和聚合:
select r.cedula
from Reserva r
where r.fechainicio >= trunc(sysdate, 'YYYY') - interval '1' year and
f.fechainicio < trunc(sysdate, 'YYYY')
group by r.cedula
having count(distinct trunc(r.fechainicio, 'MM')) = 12;