表DailyRecord 字段有 Job号,Total(产量),Pass(通过),TransDate(生产时间)等每天会有很多Job产生  问题:输入一个时间eg:2009-05-21 一个时间间隔60 
结果:输出自2009-05-21开始 往前60天的数据
具体操作:首先应该是将每天的数据分组 
然后将 自2009-05-21前60天的数据读出来  注意不包括没有产量的日期请问大家这个sql 怎么写?

解决方案 »

  1.   

    select *
    from DailyRecord 
    where 生产时间 between dateadd(day, -59, 生产时间) and 生产时间
      

  2.   

    select *
    from DailyRecord 
    where 生产时间 between dateadd(day, -59, '2009-05-21') and '2009-05-21'
      and Total>0
      

  3.   

    select * from DailyRecord 
    where 生产时间 between dateadd(day, -59, '2009-05-21') and '2009-05-21' and Total>0
      

  4.   


    declare @t datetime
    set @t='2009-05-21 '
    select * from DailyRecord where TransDate<@t and TransDate>dateadd(day,60,@d) group by convert(char(7),TransDate,120) with rollup
      

  5.   

    [code=SQL]---这个???
    select *
    from DailyRecord 
    where 生产时间 between dateadd(day, 59, 生产时间) and 生产时间 [/code]
      

  6.   

    select top 60 *
    from DailyRecord 
    where 生产时间<='2009-05-21'
      and Total>0
      

  7.   

    写错 ;修改:
    select * from DailyRecord 
    where 生产时间 between dateadd(day, 59, '2009-05-21') and '2009-05-21' and Total>0
      

  8.   


    declare @t datetime
    set @t='2009-05-21 '
    select top 60 * from DailyRecord where TransDate<@t order by TransDate desc
      

  9.   

    ...--this:
    select top 60 *
    from DailyRecord 
    where 生产时间<='2009-05-21'
      and Total>0
      

  10.   

    用IF EXISTS()判断一下行不??
      

  11.   


    select * from DailyRecord  where Total>0 and datediff(dd, TransDate,'2009-05-21 23:59:59')<=60--2009-05-21 和 60 作為參數傳進來,23:59:59 自己添加,不添加這個無法取得當天的資料
      

  12.   

    不好意思 这个sql语句还是有问题
    select top 60 *
    from DailyRecord 
    where 生产时间<='2009-05-21'
      and Total>0比如 '2009-05-20' 当天有 60个Job的产量  那就是只有 '2009-05-21'  这一天的数据了 不过我已经清楚了 先分组 然后再用这个sql 语句
      

  13.   

    select
      a.生产时间,
      sum(b.产量) as 产量
    from
    (
      select distinct top 60 convert(varchar(10),生产时间,120) as 生产时间 from tb where 生产时间<'2009-05-21' order by 生产时间 desc
    ) a,
      b
    where
      datediff(day,a.生产时间,b.生产时间)=0
    group by
      a.生产时间 
      
      

  14.   

    select top 60 *
    from DailyRecord 
    where 生产时间<='2009-05-21'
      and Total>0
    order by 生产时间 desc
      

  15.   

    SQL codeselect
      a.生产时间,
      sum(b.产量) as 产量
    from
    (
      select distinct top 60 convert(varchar(10),生产时间,120) as 生产时间 from tb where 生产时间<'2009-05-21' order by 生产时间 desc
    ) a,
      b
    where
      datediff(day,a.生产时间,b.生产时间)=0
    group by
      a.生产时间 这个sql的问题 是 把每天的 数据distinct了  可是 每天的数据我需要sum   然后 从2009-05-21 往前60个有数据的日期我还要sum