解决方案 »

  1.   


    时间类型是int 我怎么区分是前后?
      

  2.   


    --> 测试数据:@T
    declare @T table([商家id] int,[时间值] int)
    insert @T
    select 1,2 union all
    select 1,4 union all
    select 1,5 union all
    select 1,6 union all
    select 1,7 union all
    select 2,8 union all
    select 2,4 union all
    select 2,1 union all
    select 2,2 union all
    select 2,4;WITH m1 AS 
    ( SELECT ROW_NUMBER() OVER ( PARTITION BY 商家id ORDER BY GETDATE() ) AS Num , * FROM @T ),
     m2 AS ( SELECT  * FROM m1 WHERE num <= 2 )
    SELECT  
    商家id , AVG(时间值) AS 平均值
    FROM m2 GROUP BY 商家id 
    /*
    商家id        平均值
    ----------- -----------
    1           3
    2           6
    */
    这是前2次的平均值
      

  3.   

    先根据ID找出50条记录,再求平均值 
    SELECT  avg(时间)  FROM   (select top 50 * from table where id='厂家' order by 时间 desc) 
      

  4.   

    就是每一个商家取前50条ord_spendtime数据,那个sql语句能够再具体点吗,这我还是不懂??
      

  5.   

    根据sho_id获取每个商家最近20份订单的ord_sendtime,取AVG(),并将其值取出来!具体的sql语句!
      

  6.   

    SELECT  avg(ord_spendtime)  FROM   (select top 20 * from table where sho_id='11' )  //取出11编号的商家的20条记录
      

  7.   

    我记得有个avg函数的,还有sum,count,其他不记得了
      

  8.   


    -->借用叶子的数据
    --> 测试数据:@T
    declare @T table(shop_id int,orderid int,ord_sendtime int)
    insert @T
    select 1,2,20 union all
    select 1,4,40 union all
    select 1,5,50 union all
    select 1,6,60 union all
    select 1,7,70 union all
    select 2,8,80 union all
    select 2,9,40 union all
    select 2,1,10 union all
    select 2,3,20 union all
    select 2,4,40--SELECT * FROM @t;;WITH cte AS (
    SELECT rn=ROW_NUMBER() OVER(PARTITION BY shop_id ORDER BY orderid asc),* FROM @T 
    )
    SELECT shop_id,AVG([ord_sendtime]) AS 平均时间 FROM cte WHERE rn<=50 GROUP BY shop_id
      

  9.   

    那个懂了!!现在就想问一下:那个ord_sendtime (int) 想把值为0排除,然后算平均值!具体怎么做???在线等待!!!
      

  10.   

    算ord_sendtime的平均值!!加分30!!求sql语句!!
      

  11.   

     大家 我知道了!!!avg(nullif(ord_sendtime,0))