# How to calculate statistical data

**URL:** <https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332>\
**Category:** Developer\
**Created:** [February 14, 2020, 8:14pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332 "2020-02-14T20:14:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincenzo](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/vincenzo/32/847_2.png) [@Vincenzo](https://discuss.tryton.org/u/Vincenzo)\
**Post date:** [February 14, 2020, 8:14pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/1 "2020-02-14T20:14:41Z")

</div>

hello everyone,  
how do calculate the total (of a year) of the quantities of a product sent to a location (\> 10000 moves)? Is there a specific method to have this type of data (statistics)?  
Other question: calculate the quantities of product sent to a location divided by month and by type of document (customer shipment, internal shipment, customer return shipment)  
Thanks

---

<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:** [February 15, 2020, 12:21am UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/2 "2020-02-15T00:21:51Z")

</div>

> [@Vincenzo](#):
>
> how do calculate the total (of a year) of the quantities of a product sent to a location (\> 10000 moves)?

You must write a SQL query like:

```SQL
SELECT SUM(internal_quantity) FROM stock_move WHERE to_location = ? AND product = ? AND state = 'done';

```

> [@Vincenzo](#):
>
> Other question: calculate the quantities of product sent to a location divided by month and by type of document (customer shipment, internal shipment, customer return shipment)

You can add a `GROUP BY` clause.

---

<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:** [February 15, 2020, 1:50pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/3 "2020-02-15T13:50:05Z")

</div>

More over, you can use this query in [`ModelSQL.table_query`](https://docs.tryton.org/projects/server/en/latest/ref/models/models.html#trytond.model.ModelSQL.table_query) to display the result in the client.

---

<div class="post-metadata">

**Author:** ![Vincenzo](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/vincenzo/32/847_2.png) [@Vincenzo](https://discuss.tryton.org/u/Vincenzo)\
**Post date:** [February 15, 2020, 5:43pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/4 "2020-02-15T17:43:04Z")

</div>

Can it be used via RPC?  
If YES, with which model?

---

<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:** [February 15, 2020, 5:44pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/5 "2020-02-15T17:44:17Z")

</div>

A ModelSQL with a table\_query behaves exactly like any other ModelSQL (except for create, write and delete which are forbidden).

---

<div class="post-metadata">

**Author:** ![Vincenzo](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/vincenzo/32/847_2.png) [@Vincenzo](https://discuss.tryton.org/u/Vincenzo)\
**Post date:** [February 16, 2020, 10:09am UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/6 "2020-02-16T10:09:01Z")

</div>

I never used “ModelSQL”. I’ve always used “ModelStorage”.

Usually through RPC, with “ModelStorage”, I use the syntax:

`model.stock.move.search ([('product', '=', 8), ('to_location', '=', 259), ('state', '=', 'done')], 0, 1000, None, <CONTEXT>)`

If I wanted to use “ModelSql” what is the syntax to use?

I have tried with:

`model.table_query (['SELECT SUM (internal_quantity) FROM stock_move WHERE to_location = 259 AND product = 8 AND state = "done"'], <CONTEXT>)`

But I get nothing …

---

<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:** [February 16, 2020, 10:27am UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/7 "2020-02-16T10:27:03Z")

</div>

ModelSQL inherits from ModelStorage. Indeed it is an implementation of the ModelStorage for the SQL backend. So you just need to use the same API.

By the way, `stock.move` is a ModelSQL subclass.

---

<div class="post-metadata">

**Author:** ![Vincenzo](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/vincenzo/32/847_2.png) [@Vincenzo](https://discuss.tryton.org/u/Vincenzo)\
**Post date:** [February 16, 2020, 5:37pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/8 "2020-02-16T17:37:52Z")

</div>

I’m sorry, but I can’t get results …  
I can’t find examples of using table\_query.

I tried to use:

`model.stock.move.table_query (['SELECT SUM (internal_quantity) FROM stock_move WHERE to_location = 259 AND product = 8 AND state = "done"'], <CONTEXT>)`

but I don’t get results.

What is the correct command syntax? 😔  
Could you give me an example that I can test?

Thanks

---

<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:** [February 16, 2020, 5:48pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/9 "2020-02-16T17:48:29Z")

</div>

You must define a new ModelSQL that you will query.  
Here is an example: [https://hg.tryton.org/modules/timesheet/file/ee4b51fe0754/line.py#l230](https://hg.tryton.org/modules/timesheet/file/ee4b51fe0754/line.py#l230)

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [March 17, 2020, 5:55pm UTC](https://discuss.tryton.org/t/how-to-calculate-statistical-data/2332/10 "2020-03-17T17:55:47Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
