选择具有最近日期的行

时间:2017-06-19 03:15:55

标签: mysql sql greatest-n-per-group

样本表数据:

brew install openssl
rvm reinstall 2.3.4 --with-openssl-dir=`brew --prefix openssl`

pseduo:

  • 获取所有+---------+--------+-------+---------------------+----------+ | ref | cost | stock | date | location | +---------+--------+-------+---------------------+----------+ | 00AD075 | 352.33 | 6 | 2017-06-14 08:24:43 | WA | | 00AD075 | 352.33 | 4 | 2017-06-19 06:01:14 | WA | | 00AD075 | 352.33 | 0 | 2017-06-22 00:00:00 | WA | | 00AE912 | 231.46 | 2 | 2017-06-14 08:24:43 | WA | | 00AE912 | 231.46 | 1 | 2017-06-15 06:31:03 | WA | | 00AE912 | 231.46 | 2 | 2017-06-16 06:01:16 | NY | | 00AE912 | 231.46 | 0 | 2017-06-19 00:00:00 | WA | | 00AE938 | 852.25 | 2 | 2017-06-14 08:24:43 | NY | | 00AG510 | 172.27 | 1 | 2017-06-14 08:24:43 | NY | | 00AG520 | 359.67 | 6 | 2017-06-14 08:24:43 | NY | | 00AG590 | 747.21 | 1 | 2017-06-14 08:24:43 | CA | | 00AG590 | 550.00 | 1 | 2017-06-15 06:31:03 | CA | +---------+--------+-------+---------------------+----------+
  • 的地方
  • ref LIKE '00%' AND stock > 0 DESC
  • 排序
  • 保留最新信息并丢弃所有其他具有相同位置的行
  • 如果股票大于0则丢弃它

预期数据:

date

因此,根据+----------+----------+---------+-----------------------+-----+ | 00AD075 | 352.33 | 4 | 2017-06-19 06:01:14 | WA | +----------+----------+---------+-----------------------+-----+ | 00AE912 | 231.46 | 2 | 2017-06-16 06:01:16 | NY | | 00AE938 | 852.25 | 2 | 2017-06-14 08:24:43 | NY | | 00AG510 | 172.27 | 1 | 2017-06-14 08:24:43 | NY | | 00AG520 | 359.67 | 6 | 2017-06-14 08:24:43 | NY | | 00AG590 | 550.00 | 1 | 2017-06-15 06:31:03 | CA | +----------+----------+---------+-----------------------+-----+ ,每个ref有多行。我需要选择最近的一行。这就是我正在做的,但它给了我一个错误:

date

错误:

  

1055 - SELECT列表的表达式#2不在GROUP BY子句中   包含非聚合列'inventory.scost',它不是   功能上依赖于GROUP BY子句中的列;这是   与sql_mode = only_full_group_by不兼容,时间:0.319000s

2 个答案:

答案 0 :(得分:3)

已更新在启用严格模式的情况下执行此操作的一种方法

L.Control.opacitySlider = L.Control.extend({
    options: {
        position: 'topright'
    },
    setOpacityLayer: function (layer) {
            opacity_layer = layer;
    },
    onAdd: function (map) {
        var opacity_slider_div = L.DomUtil.create('div', 'opacity_slider_control');

        $(opacity_slider_div).slider({
          orientation: "vertical",
          range: "min",
          min: 0,
          max: 100,
          value: 60,
          step: 10,
          start: function ( event, ui) {
            //When moving the slider, disable panning.
            map.dragging.disable();
            map.once('mousedown', function (e) { 
              map.dragging.enable();
            });
          },
          slide: function ( event, ui ) {
            var slider_value = ui.value / 100;
            opacity_layer.setOpacity(slider_value);
          }
        });        
        return opacity_slider_div;
    }
});

或者您可以使用会话变量模拟SELECT i.* FROM inventory i JOIN ( -- fetch the ref and the max date SELECT ref, MAX(date) date FROM inventory WHERE ref LIKE '00%' AND stock > 0 GROUP BY ref ) q -- then join back to grab all other columns ON i.ref = q.ref AND i.date = q.date ORDER BY date, ref ,因为MySQL还不支持窗口函数

ROW_NUMBER() OVER (PARTITION BY ref ORDER BY date)

输出:

+---------+------+-------+---------------------+----------+
| ref     | cost | stock | date                | location |
+---------+------+-------+---------------------+----------+
| 00AE938 |  852 |     2 | 2017-06-14 08:24:43 | NY       |
| 00AG510 |  172 |     1 | 2017-06-14 08:24:43 | NY       |
| 00AG520 |  360 |     6 | 2017-06-14 08:24:43 | NY       |
| 00AG590 |  550 |     1 | 2017-06-15 06:31:03 | CA       |
| 00AE912 |  231 |    20 | 2017-06-16 06:01:16 | WA       |
| 00AD075 |  352 |     4 | 2017-06-19 06:01:14 | WA       |
+---------+------+-------+---------------------+----------+

以下是两个查询的dbfiddle演示

答案 1 :(得分:0)

SELECT DISTINCT
    * 
FROM
    inventory 
GROUP BY
    ref 
HAVING 
    ref LIKE '00%' 
    AND stock > 0 
ORDER BY
    date desc

试试这个。