我想知道八月份每个日期的平均数据数量。
示例
31-08 - 2 registers
30-08 - 3 registers
我尝试了以下查询,但没有成功
SELECT AVG(ROW_NUMBER() over (order by Chave_ID desc)), DATEPART(day, datahora)
from San_Chave
where DATEPART(month, datahora) = 8
group by DATEPART(day, datahora), Chave_ID
order by DATEPART(day, datahora) desc
DataHora是DateTime
类型和
Chave_Id为Primary Key
,但数字不符合逻辑顺序,因此我尝试使用ROW_NUMBER()
函数
有人可以帮助我吗?
我想要的输出
MONTH DAY REGISTER
8 31 10
8 30 9
8 29 1
表格
要执行查询,我认为只需要Chave_Id
和DataHora
,Chave_id
将是寄存器的数量
CREATE TABLE [dbo].[San_Chave](
[Chave_Id] [int] IDENTITY(1,1) NOT NULL,
[Usuario_Id] [int] NULL,
[Credenciada_Id] [int] NULL,
[Usuario_Id_Responsavel] [int] NULL,
[DataHora] [datetime] NULL,
[Transacao] [int] NULL,
[Cliente_Id] [int] NULL,
[DataHoraPegou] [datetime] NULL,
[DataHoraDevolverPrevisao] [datetime] NULL,
[DataHoraEntregou] [datetime] NULL,
[HorasDevolucao] [int] NULL,
[NomeResponsavel] [varchar](130) NULL,
[CpfResponsavel] [varchar](20) NULL,
[RgResponsavel] [varchar](20) NULL,
[TelResponsavel] [varchar](15) NULL,
[Tel2Responsavel] [varchar](15) NULL,
[Endereco] [varchar](300) NULL,
[Devolvido] [bit] NULL,
[TextoDevolucao] [varchar](5000) NULL,
[De] [int] NULL,
CONSTRAINT [PK_San_Chave] PRIMARY KEY CLUSTERED
(
[Chave_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
答案 0 :(得分:0)
猜测,没有看到你的架构:
select Year, Month, avg(Count) as DailyAvgPerMonth
from (
select DATEPART(year, datahora) as Year,
DATEPART(month, datahora) as Month,
count(*) as Count
from Save_Chave
group by DATEPART(year, datahora),
DATEPART(month, datahora),
DATEPART(day, datahora)
) a
group by Year, Month
答案 1 :(得分:0)
我认为这样的事情会产生所需的输出
SELECT CAST(DataHora AS DATE) AS [Date],
COUNT(Chave_ID) AS [Entries]
FROM San_Chave
GROUP BY CAST(DataHora AS DATE)
ORDER BY [Date]