0

我在编写简洁的代码以有效地生成所需的结果时遇到了一些麻烦(在数百万条记录数据库上)。

  • 项目将按time
  • 将通过B 优先于 A(并且 C 优先于 B)来选择项目provider
  • 值必须与所选提供者的值匹配

表与想要的结果:

// given this table
id | provider | time       | value
---+----------+------------+-----------
 1 |    A     | 2013-07-01 |  0.1
 2 |    A     | 2013-07-02 |  0.2
 3 |    B     | 2013-07-02 |  0.3
 4 |    A     | 2013-07-03 |  0.4

// extrapolate this result
---+----------+------------+-----------
1  |   A      | 2013-07-01 |  0.1
3  |   B      | 2013-07-02 |  0.3
4  |   A      | 2013-07-03 |  0.4

生成表和填充数据的查询:

data_teste CREATE TABLE `data_teste` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`provider` varchar(12) NOT NULL,`time` date NOT NULL,`value` double NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `index` (`provider`,`time`),KEY `provider` (`provider`),KEY `time` (`time`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO data_teste(`provider`, `time`, `value`) VALUES('A', '2013-07-01', 0.1),('A', '2013-07-02', 0.2),('B', '2013-07-02', 0.3),('A', '2013-07-03', 0.4);

这是经典的 group_by/sort 问题窗口化。

非常感谢。

4

2 回答 2

1
select d.* 
from data_teste d
inner join
(
   select `time`, max(provider) mp
   from data_teste
   group by `time`
) x on x.mp = d.provider 
    and x.`time` = d.`time`
order by  `time` asc, 
          provider desc
于 2013-07-18T17:13:23.203 回答
0

这表现如何?

SELECT 
  *
FROM 
  `data_teste` dt1 
   LEFT JOIN `data_teste` dt2 ON ( dt2.time = dt1.time 
                                    AND dt2.provider > dt1.provider )
WHERE 
  dt2.ID IS NULL
于 2013-07-18T17:31:46.263 回答