azy
azy

Reputation: 153

PostgreSQL query to check minimum and maximum data

i have the example database here,

table products

id |  name  |  code  |  minimum_stock  |  stock  |  maximum_stock  
1     AAA      AAA            50           75           100
2     BBB      BBB            70           50           300
3     CCC      CCC            100          200          150
4     DDD      DDD            40           25           100
5     EEE      EEE            70           10           100

in this case i want to show only data who is stock exceed compared by their minimum_stock or maximum_stock(below or above their minimum maximum stock) and also i want to show datain safe stock position (between minimum and maximum stock), stock value in here is a result from aggregate sum function

if i want only show data exceed minimum stock, the result output must be like this

id |  name  |  code  |  **minimum_stock**  |  **stock**  |  maximum_stock  
2     BBB      BBB            **70**           **50**           300
4     DDD      DDD            **40**           **25**           100
5     EEE      EEE            **70**           **10**           100

and if i want only show data exceed maximum stock, the result output must be like this

id |  name  |  code  |  minimum_stock  |  **stock**  |  **maximum_stock**  
3     CCC      CCC            100          **200**          **150**

and the last one the stock between minimum and maximum stock, it must be like this

id |  name  |  code  |  **minimum_stock**  |  **stock**  |  **maximum_stock**  
1     AAA      AAA            **50**           **75**           **100**

how can I do that?

Upvotes: 0

Views: 119

Answers (1)

SimarjeetSingh Panghlia
SimarjeetSingh Panghlia

Reputation: 2200

Try this

Minimum Stock

Select * from table where stock < minimum_stock

maximum stock

Select * from table where stock > maximum_stock

between minimum and maximum stock

Select * from table where stock < maximum_stock and stock > minimum_stock

Upvotes: 1

Related Questions