0

我想知道八月每个日期的平均数据数量。

例子

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_Idand DataHoraChave_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
4

2 回答 2

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
于 2012-09-21T14:46:42.547 回答
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]
于 2012-09-21T15:03:38.000 回答