select  convert(varchar(10),Cre_Dat,120) as 日期,count(*) as 记录条数,
      sum(In_Coin) as In_Coin,sum(Out_Coin) as Out_Coin
from 你的表
where month(Cre_Dat)=month(getdate())  --當前月份
group by convert(varchar(10),Cre_Dat,120)

解决方案 »

  1.   

    convert(varchar(10),Cre_Dat,120) 是取日期的YYYY--MM--DD數據,如:select convert(varchar(10),getdate(),120)
    --返回 2004-04-02
      

  2.   

    要求不用程序控制,数据全部统计在一个记录集里
    像这样:日期    In_Coin    Out_Coin    当天记录数
    1       1500       1200        300
    2       0          0           0
    3       2000       1500        500
    4       ……
      

  3.   

    --按月查询的函数
    create function f_qry(@年月 varchar(6))
    returns table
    as
    return(
    select a.日期,记录条数=isnull(记录条数,0)
    ,In_Coin=isnull(In_Coin,0),Out_Coin=isnull(Out_Coin,0)
    from(
    select 日期=convert(varchar(10),dateadd(day,a.id+b.id,@年月+'01'),120)
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    ) a,(
    select id=0 union all select 6
    union all select id=12 union all select 18
    union all select id=24 union all select 30
    ) b
    )a left join(
    select 日期=convert(varchar(10),Cre_Dat,120)
    ,记录条数=count(*)
    ,In_Coin=sum(In_Coin),Out_Coin=sum(Out_Coin)
    from 表
    group by convert(char(10),Cre_Dat,120)
    )b on a.日期=b.日期
    where datediff(month,日期,@年月+'01')=0
    )
    go
    --调用示例
    select * from f_qry('200302')
      

  4.   

    --按你最后的结果要求改一下:create function f_qry(@年月 varchar(6))
    returns table
    as
    return(
    select 日期=a.id+1
    ,In_Coin=isnull(In_Coin,0)
    ,Out_Coin=isnull(Out_Coin,0)
    ,当天记录数=isnull(记录条数,0)
    from(
    select id,日期=convert(varchar(10),dateadd(day,a.id+b.id,@年月+'01'),120)
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    ) a,(
    select id=0 union all select 6
    union all select id=12 union all select 18
    union all select id=24 union all select 30
    ) b
    )a left join(
    select 日期=convert(varchar(10),Cre_Dat,120)
    ,记录条数=count(*)
    ,In_Coin=sum(In_Coin),Out_Coin=sum(Out_Coin)
    from 表
    group by convert(char(10),Cre_Dat,120)
    )b on a.日期=b.日期
    where datediff(month,日期,@年月+'01')=0
    )
    go
      

  5.   

    谢谢: zjcxc(: 邹建 :) ,我试试看!
      

  6.   

    用zjcxc的方法生成日期表,有了每月的日期表,樓主可建一job,設為每月末運行比如說日期表為#date,字段名為dt:insert into 你的月報表(對應字段...)
    select A.dt,isnull(B.记录条数,0) as 记录条数 ,  isnull(B.In_Coin,0) as In_Coin,  isnull(B.Out_Coin,0) as Out_Coin
    from #date A
    left join
    (select  convert(varchar(10),Cre_Dat,120) as 日期,count(*) as 记录条数,
          sum(In_Coin) as In_Coin,sum(Out_Coin) as Out_Coin
    from 你的表
    where month(Cre_Dat)=month(getdate())  --當前月份
    group by convert(varchar(10),Cre_Dat,120)
    ) as 
    B on B.日期=A.convert(varchar(10),dt,120)
      

  7.   

    使用作业(job),以後的報表可自動生成,如何创建請參考以下代碼,有事先下了。--创建作业exec msdb..sp_add_job @job_name='结束',@delete_level=3--创建作业步骤
    declare @sql varchar(800)
    select @sql='exec SP_PAS_DBQB_END ''20040401'',''SJ'''
    exec msdb..sp_add_jobstep @job_name='结束',
    @step_name = '20040401',
    @subsystem = 'TSQL',
    @database_name='MYDB',
    @command = @sql,
    @retry_attempts = 0--创建调度
    EXEC msdb..sp_add_jobschedule @job_name = '结束', 
    @name = '调度',
    @freq_type=1,
    @freq_subday_type=0x1,
    @active_start_date =20040401,
    @active_start_time =112140
    -- 添加目标服务器
    EXEC msdb.dbo.sp_add_jobserver 
    @job_name = '结束' ,
    @server_name = N'(local)' 
      

  8.   

    declare @ datetime
    set @ = '2004-4-6'
    select min(dateadd(day,n.i,dateadd(month,datediff(month,0,@),0)))
    ,sum(In_Coin),sum(Out_Coin),count(*)from T right join(
    select 0 as i
    union all
    select 1 
    union all
    select 2
    union all
    select 3
    union all
    select 4
    union all
    select 5
    union all
    select 6
    union all
    select 7
    union all
    select 8
    union all
    select 9
    union all
    select 10
    union all
    select 11
    union all
    select 12
    union all
    select 13
    union all
    select 14
    union all
    select 15
    union all
    select 16
    union all
    select 17
    union all
    select 18
    union all
    select 19
    union all
    select 20
    union all
    select 21
    union all
    select 22
    union all
    select 23
    union all
    select 24
    union all
    select 25
    union all
    select 26
    union all
    select 27
    union all
    select 28
    union all
    select 29
    union all
    select 30
    union all
    select 31
    ) N
    on datediff(day,dateadd(day,n.i,dateadd(month,datediff(month,0,@),0)),T.cre_dat)=0  
    where datediff(month,dateadd(day,n.i,dateadd(month,datediff(month,0,@),0)),@)=0
    group by datediff(day,0,dateadd(day,n.i,dateadd(month,datediff(month,0,@),0)))
      

  9.   

    playyuer(双规干部) ,
    (1)您的查询有问题,选2月份查到3月一号的数据了。
    (2)月份没有用,每个月的数据都一样。~~
      

  10.   

    嘿嘿 playyuer(双规干部) 不好意思,是我搞错了,已经调好:)