每天根据最大值组加入表

时间:2018-02-04 07:30:03

标签: mysql

表observasi_data

 _________________      
| obsid  | value4 |       
|-------+--------|       
|  4085 |    0   |        
|  4086 |   10   |      
|  4087 |   10   |       
|  4088 |   10   |       
|  4089 |   0    |       
|  4090 |   20   |       
|  4091 |   10   |             
|  4093 |   50   |

表观察

__________________       
| obsid | date   |       
|-------+--------|       
|  4085 |8/1/09  |        
|  4086 |8/1/09  |       
|  4087 |8/1/09  |       
|  4088 |9/1/09  |       
|  4089 |9/1/09  |       
|  4090 |9/1/09  |       
|  4091 |10/1/09 |             
|  4093 |10/1/09 |

然后我有这样的查询

SELECT  a.obsid, a.value4, b.date
          FROM observasi_data a
          INNER JOIN observasi b ON a.obsid = b.obsid

          ORDER BY `b`.`date` ASC

然后输出就像那样,

     ___________________________       
    | obsid  | value4 | date   |       
    |-------+--------|--------|       
    |  4085 |    0   |8/1/09  |        
    |  4086 |   10   |8/1/09  |       
    |  4087 |   10   |8/1/09  |       
    |  4088 |   10   |9/1/09  |       
    |  4089 |   0    |9/1/09  |       
    |  4090 |   20   |9/1/09  |       
    |  4091 |   10   |10/1/09 |             
    |  4093 |   50   |10/1/09 |

现在,我想找到 MAX value4每个日期,所以我使用了那个查询

SELECT  a.obsid,MAX(a.value4), b.date
              FROM observasi_data a
              INNER JOIN observasi b ON a.obsid = b.obsid

              GROUP BY b.date
              ORDER BY `b`.`date` ASC

但输出变得像那样

| obsid  | max(value4) | date   |       
|-------+-------------|--------|       
|  4085 |   10        |8/1/09  |        
|  4088 |   20        |9/1/09  |       
|  4091 |   50        |10/1/09 |  

如您所见,我的每个日期都有最大值4,但是'obsid'列中的数据不匹配的值为4,我曾尝试过,但直到现在我还没有得到溶液

1 个答案:

答案 0 :(得分:0)

如果你也需要这个黑暗,这是一种方式。

Select a.obsid,a.value4,b.date from observasi_data a INNER JOIN observasi b ON a.obsid=b.obsid  where a.obsid IN (Select obsid from new1 where value4 IN (Select MAX(a.value4) from new1 a INNER JOIN new2 b ON a.obsid = b.obsid group by b.date order by b.date asc));