Skip to main content

Query to find Nth Maximum and Minimum value from the Table

Hi,
This query will fetch the rows which is the most large value / maximum value.
--Maximum value
SELECT MAX( spend )
  FROM edw_aggregate.vw_agg_order_automation
 WHERE spend < ( SELECT MAX( spend )
                 FROM edw_aggregate.vw_agg_order_automation );


The above query will only fetch top most value from the table. If we want to fetch Nth  maximum / minimum value, look into this query.
 
--Nth Minimum/Max
SELECT MIN( spend )
  FROM edw_aggregate.vw_agg_order_automation
  where spend in (SELECT top 5 spend FROM edw_aggregate.vw_agg_order_automation order by spend desc)


Thanks

Comments