简体   繁体   中英

hive sql get min and max values of multiple records

I have a query which results are

fruit           street             inventory            need      to_buy
banana          123                15                   99        22
apple           4                  32                   68        44
banana          789                01                   32        11
apple           9832               0                    99        94
apple           85                 839                  12        48
banana          832                77                   05        55

I want to get the minimum values for inventory, and need, and get the max to_buy value. but only have one record of each 'fruit'. the 'street' column is irrelevant and is not needed in the final result. The final result should look like

fruit            inventory(min)            need(min)      to_buy(max)
banana           01                        05             55
apple            0                         12             94

Also the initial records may not be ordered at first so there are more 'fruits' inserted at random How can i achieve the desired result above?

Try this:

SELECT MIN(inventory), MIN(need), MAX(to_buy)
FROM tableName
GROUP BY fruits

This one should work:

SELECT fruits, MIN(inventory), MIN(need), MAX(to_buy)
FROM <table_name>
GROUP BY fruits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM