Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL/ Return MIN values of multiple rows

I’m trying to get the minimum value of open, across multiple rows of year. This is from app.mode.com and the site only says SQL, not sure which version

SELECT year,
    open
  FROM tutorial.aapl_historical_stock_price
WHERE open = (select MIN(open)
  FROM tutorial.aapl_historical_stock_price)

When I use the code above, the result is
Table result vs actual output
| Year | Open |
|—— |——|
|2000 | 0 |
|2000 | 0 |
|2000 | 0 |

What I’m trying to get is
| Year | Open |
|—— |——|
|2002 | 0 |
|2001 | 0 |
|2000 | 0 |
Can someone help point me what I’m doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

select year and get the min by grouping each year as following:

select 
    year
    , min(open) as <desired_alias>
from your_table
group by 1
order by 1 desc;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading