0

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?

1
  • do you want three queries or one? are the ** part of the requred output or just for emphasis? Commented Jan 14, 2015 at 6:08

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

how about the stock is a result from sum operation, i tried it but its error
i got this message ERROR: aggregates not allowed in WHERE clause
@azy use HAVING instead of WHERE if there are agregates in the condition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.