要求是这样的,有两个表,一个主表的记录是
TabA
id   name 
 1     a  
 2     b
 3     c一个记录表
TabB
id    value    time 
1      0        t1
2      0        t2
1      1        t3
2      1        t4
3      0        t5
1      0        t6
1      1        t7
这个表的记录是:对一个id而言, value 在 0,1,0,1之间不停的变化,后面的事件记录每次变化的时间点
现在要统计比如
a
b
c  他们 1 到0; 和0到1 的时间比:
1表示错误,0表示错误恢复
就是出错时间和正常时间的比
有答案验证了立刻给分;谢谢!    

解决方案 »

  1.   

    结果:
    id    name    sum(time1)   sum(tim2)
    1      a        60           40
    2      b        30           70
    3      c        80           20  
      

  2.   


    declare @TabA table (id int,name varchar(1))
    insert into @TabA
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'declare @TabB table (id int,value int,time int)
    insert into @TabB
    select 1,0,5 union all
    select 2,0,6 union all
    select 1,1,7 union all
    select 2,1,8 union all
    select 3,0,9 union all
    select 1,0,11 union all
    select 1,1,12select id,
    [0的时间]=SUM(CASE [value] WHEN 1 THEN time else 0 end) ,
    [1的时间]=SUM(CASE [value] WHEN 0 THEN time else 0 end) ,
    比例=CAST(SUM(CASE [value] WHEN 1 THEN time ELSE 0 end) AS DECIMAL(18,4))/SUM(CASE [value] WHEN 0 THEN [time] else 0 end)
    from @TabB GROUP BY id
    /*
    id          0的时间        1的时间        比例
    ----------- ----------- ----------- ---------------------------------------
    1           19          16          1.187500000000000
    2           8           6           1.333333333333333
    3           0           9           0.000000000000000
    */
      

  3.   

    declare @TabA table (id int,name varchar(1))
    insert into @TabA
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'declare @TabB table (id int,value int,time int)
    insert into @TabB
    select 1,0,5 union all
    select 2,0,6 union all
    select 1,1,7 union all
    select 2,1,8 union all
    select 3,0,9 union all
    select 1,0,11 union all
    select 1,1,12SELECT a.*,b.[0的时间],b.[1的时间] FROM @TabA a
    LEFT JOIN(
    select id,
    [0的时间]=SUM(CASE [value] WHEN 1 THEN time else 0 end) ,
    [1的时间]=SUM(CASE [value] WHEN 0 THEN time else 0 end) ,
    比例=CAST(SUM(CASE [value] WHEN 1 THEN time ELSE 0 end) AS DECIMAL(18,4))/SUM(CASE [value] WHEN 0 THEN [time] else 0 end)
    from @TabB GROUP BY id
    ) b 
    ON a.id=b.id
    /*
    id          name 0的时间        1的时间
    ----------- ---- ----------- -----------
    1           a    19          16
    2           b    8           6
    3           c    0           9
    */
      

  4.   

    你找个结果不对吧a的 1-0 只有一次吧, 0-1有两次id value time  
    1 0 t1
    2 0 t2
    1 1 t3
    2 1 t4
    3 0 t5
    1 0 t6
    1 1 t7
      

  5.   

    declare @TabA table (id int,name varchar(1))
    insert into @TabA
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'declare @TabB table (id int,value int,time int)
    insert into @TabB
    select 1,0,5 union all
    select 2,0,6 union all
    select 1,1,7 union all
    select 2,1,8 union all
    select 3,0,9 union all
    select 1,0,11 union all
    select 1,1,12select a.id,a.name,cast((sum(value)-1)*100.0/(count(a.id)-1) as int)[1-0],
    cast((1-(sum(value)-1)*1.0/(count(a.id)-1))*100 as int)[0-1]
    from @TabA a,@TabB b
    where a.id=b.id
    group by a.id,a.name
    --根据以上数据。
    --id=1 的变化了三次 即 0-1-0-1  1变0 只有一次所以是33%
    --id=2 的之变化了一次 即从0变为1
    --id=3的未变化,所以就没有变化的比例
    /*
    id          name 1-0         0-1
    ----------- ---- ----------- -----------
    1           a    33          66
    2           b    0           100
    */
      

  6.   

    你的语句没有问题;我还有个小疑问:
    @TabB  这个表的value 不是整形;是字符串,不如0,1对应的'up' 'down'
    time 是一个时间类型;表示时间点
    统计的是时间的长度
      

  7.   


    我的没问题还是叶子的?sum(case when value ='up' then 1 else 0 end)
      

  8.   


    select 
    a.*,
    [1的时间]=(select [1的时间]=SUM(CASE [value] WHEN 1 THEN time else 0 end) from @TabB where id=a.id),
    [0的时间]=(select [0的时间]=SUM(CASE [value] WHEN 0 THEN time else 0 end) from @TabB where id=a.id)  
    From @TabA a
      

  9.   


    接用 (北漂☆叶子)  数据 declare @TabA table (id int,name varchar(1))
    insert into @TabA
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'declare @TabB table (id int,value int,time int)
    insert into @TabB
    select 1,0,5 union all
    select 2,0,6 union all
    select 1,1,7 union all
    select 2,1,8 union all
    select 3,0,9 union all
    select 1,0,11 union all
    select 1,1,12
    =====================select 
    a.*,
    [1的时间]=(select [1的时间]=SUM(CASE [value] WHEN 1 THEN time else 0 end) from @TabB where id=a.id),
    [0的时间]=(select [0的时间]=SUM(CASE [value] WHEN 0 THEN time else 0 end) from @TabB where id=a.id)  
    From @TabA a
      

  10.   


    --状态保持表
    Select t1.id, t1.value,
        t1.time as from_time,
        t2.time as to_time
    from
        TabB t1
        inner Join
        TabB t2 on t2.id=t1.id 
            and t2.time = (select min(time) from tabB where id=t1.id and time>t1.time)
            --然后利用这个状态保持表来计算各个value的时间比例,应该不难了吧?要注意的是如果时间范围过长的话,使用Date_Diff函数可能会溢出