# Query vs Search for a function field

**URL:** https://discuss.tryton.org/t/query-vs-search-for-a-function-field/3296
**Category:** Developer
**Created:** [September 28, 2020, 7:59pm UTC](https://discuss.tryton.org/t/query-vs-search-for-a-function-field/3296 "2020-09-28T19:59:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [September 28, 2020, 7:59pm UTC](https://discuss.tryton.org/t/query-vs-search-for-a-function-field/3296/1 "2020-09-28T19:59:14Z")

</div>

Hi,  
I need to add a field on product model to get the last cost price of the previous month. (taken from product\_cost\_history)  
Some questions:

- I intend to use a function field, do think it’s the best choice ?
- To get the last cost price of the previous month, is it better to use a query or is it possible to do this with a search ?  
Thanks for advices/help ! 🙂

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [September 28, 2020, 9:54pm UTC](https://discuss.tryton.org/t/query-vs-search-for-a-function-field/3296/2 "2020-09-28T21:54:31Z")

</div>

If you want it to be performant, you should go for a SQL query because ORM can do that only one product at a time.  
With SQL query, you can use a `LAST_VALUE` window function. You can find an example in `Currency.currency_rate_sql`.
