MySQL Forums
Forum List  »  General

Dynamic filter clause
Posted by: Manish Gurnani
Date: September 13, 2015 10:50PM

I have a 2 tables with sample data as below:

Table Stock :

StockId,Price,Volume

1,100.0,1000O

2,200.0,20000

Table Triggers :

StockId, minPrice, maxPrice, minVolume, maxVolume

1,80.0,90.0,null,11000

1,null,90.0,null,null

2,null,250,0,null,null

Basically Triggers is used to store filter criteria. A matching criteria would be whenever Price is below minPrice or above maxPrice. Similarly for Volume. Any Null values are to be ignored. Stock table has unique StockIds while Triggers could have duplicates.

I want to retrieve rows from Triggers whenever ALL criteria are met for a stock. eg: in given data, row 2 of Triggers would match (100 > 90 and no volume criteria)..

I tried using query like this but it didn't work:

select t.StockId from Triggers t, Stock s where t.stockId = s.stockId and
(((t.minPrice IS NOT NULL and s.Price <= t.minPrice)
or ( t.maxPrice IS NOT NULL and s.Price >= t.maxPrice))
and
( (t.minVolume IS NOT NULL and s.Volume <= t.minVolume)
OR (t.maxVolume IS NOT NULL and s.volume >= t.maxVolume))) ;

What would be the best way to get this done?

Options: ReplyQuote


Subject
Written By
Posted
Dynamic filter clause
September 13, 2015 10:50PM
September 14, 2015 12:39PM
September 14, 2015 11:15PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.