获得最低价值

时间:2015-10-21 05:47:55

标签: hive hiveql

我在Hive中有这个表test

+----------+-------+-------+
|   name   | price | notes |
+----------+-------+-------+
| product1 |   100 |       |
| product1 |   200 | note1 |
| product2 |    10 | note2 |
| product2 |     5 | note2 |
+----------+-------+-------+

我希望得到这个结果(与最低价格的产品不同)

+----------+-------+-------+
|   name   | price | notes |
+----------+-------+-------+
| product1 |   100 |       |
| product2 |     5 | note2 |
+----------+-------+-------+

由于notes中的product1不同,我无法使用以下查询。

SELECT name, MIN(price), notes
FROM test
GROUP BY name, notes;

+----------+-------+-------+
|   name   | price | notes |
+----------+-------+-------+
| product1 |   100 |       |
| product1 |   200 | note1 |
| product2 |     5 | note2 |
+----------+-------+-------+

4 个答案:

答案 0 :(得分:0)

删除分组中的notes,然后重试: -

SELECT name, MIN(price), notes
FROM test
GROUP BY name

Run Code

答案 1 :(得分:0)

试试这个

row.addEventListener('mousedown', function (event) {
   RowClick(event.target, false);
});

答案 2 :(得分:0)

您可以使用windowing functions在Hive中执行此操作。

<强>查询

select distinct name
  , min_price
  , notes
from (
  select *
    , min(price) over (partition by name) num_price
  from db.table ) x
where min_price = price

<强>输出

product1    100        
product2    5      note2

答案 3 :(得分:0)

这也可以使用子查询找到。

SELECT * FROM table1
JOIN table2 ON (table2.colunmname = table1.columnname)

上述查询将输出为:

    hive> select A.name,A.price,B.notes from (select name,min(price) as price from products group by name) as A
inner join (select name,price,notes from products) as B 
on a.name = b.name and a.price = b.price;

但是,suquery方法在同一个表上有2次迭代,不建议用于更大的表。

对于较大的表格,请参阅@GoBrewers14 answer:

product1        100
product2        5       note2