比如一张表,date      type     value                                            
            199203    a         23                   
            199206    a         12
            199209    a         13
            199212    a         14
            199303    a         34
            199201    b         3
            199202    b         4
            199203    b         5
            199204    b         3
            199205    b         6
            199206    b         6
            199207    b         4
            199208    b         3
            199209    b         2
            199210    b         3 
            199211    b         6
            199212    b         3
            199301    b         7
            199302    b         5
            199303    b         6
用MYSQL写,怎样写能的出这样的结果
            date     type    value   type1   value1
            199203   a       23        b      4
            199206   a       12        b      5
            199209   a       13        b      3
            199212   a       14        b      4
            199303   a       34        b      6也就是value1 的当date=199203 的时候  value1的值 等于type=b  date=199201,199202,199203 的value 平均值

解决方案 »

  1.   

    select  date,min(case when type='a' then value end),min(case when type='b' then value end)
    from tb
    group by date
      

  2.   

    select date ,type ,value, 'b' as type1, (select   sum(value) from (select value from 比如一张表 where type='b' and date<t.date order by date desc limit 3 ) a) as value1
    from 比如一张表 t
    where type='a'试试这个,未经测试。
      

  3.   

     select date ,type ,value, 'b' as type1, (select sum(value) from (select value from t1 where type='b' and date<t1.date order by date desc limit 3 ) a) as value1
    from  t1
    where type='a'
      这个 type,value,type1字段的值是对的,可是value1 没有数据出来呢  
      

  4.   

    SELECT d ,t ,v, 'b' AS type1, (SELECT SUM(v) FROM (SELECT v FROM test WHERE t='b' AND d<t1.d ORDER BY d DESC LIMIT 3 ) a) AS value1
    FROM test t1 WHERE t='a'Error Code : 1054
    Unknown column 't1.d' in 'where clause' SELECT d ,t ,v, 'b' AS type1, (SELECT SUM(v) FROM (SELECT v FROM test t1 WHERE t='b' AND d<t1.d ORDER BY d DESC LIMIT 3 ) a) AS value1
    FROM test t1 WHERE t='a'value1 为 (NULL)