Good morning
I am making an inquiry to know the number of rows that are currently planted and the rows that were already planted but the harvest ended and therefore the remaining plants are removed for a new planting per farm.
When there is a row planted in the database it should be in status = 1
and type = 'SW'
but the status = 2
When a furrow has already been planted and harvested it should be in status = '1'
and type = 'ER'
.
The process is as follows:
In farm 1, block 1 and groove 1, a sowing is made in the database and it remains like this;
------------------------------------------------------------------------
id | id_land | id_block | id_groove | type | date_register | status |
------------------------------------------------------------------------
1 | 1 | 1 | 1 | SW | 2015-06-12 | 1 |
------------------------------------------------------------------------
When the time has passed and it was harvested, we proceed to eliminate the plants and leave the groove empty, when registering the database it looks like this:
------------------------------------------------------------------------
id | id_land | id_block | id_groove | type | date_register | status |
------------------------------------------------------------------------
1 | 1 | 1 | 1 | SW | 2015-06-12 | 2 |
------------------------------------------------------------------------
2 | 1 | 1 | 1 | ER | 2015-06-12 | 1 |
------------------------------------------------------------------------
The state of the first sowing becomes 2 and the one of no sowing will be state 1, now if it is sown again, it is registered in the database as follows:
------------------------------------------------------------------------
id | id_land | id_block | id_groove | type | date_register | status |
------------------------------------------------------------------------
1 | 1 | 1 | 1 | SW | 2015-06-12 | 2 |
------------------------------------------------------------------------
2 | 1 | 1 | 1 | ER | 2015-06-12 | 1 |
------------------------------------------------------------------------
3 | 1 | 1 | 1 | SW | 2015-06-12 | 1 |
------------------------------------------------------------------------
The state of type 'ER'
remains with the same status = 1
and the new planting remains with status = 1
, and finally if the harvest reappears it is recorded again like this:
------------------------------------------------------------------------
id | id_land | id_block | id_groove | type | date_register | status |
------------------------------------------------------------------------
1 | 1 | 1 | 1 | SW | 2015-06-12 | 2 |
------------------------------------------------------------------------
2 | 1 | 1 | 1 | ER | 2015-06-12 | 1 |
------------------------------------------------------------------------
3 | 1 | 1 | 1 | SW | 2016-06-12 | 2 |
------------------------------------------------------------------------
4 | 1 | 1 | 1 | ER | 2016-06-12 | 1 |
------------------------------------------------------------------------
And so the process successively.
I have the following query to know the number of rows that have a block with status = 100
and type = 'ER'
SELECT id_land, COUNT(id_groove) AS units FROM sowing
WHERE type= "ER"
AND status = 100
GROUP BY id_land;
In this query it brings all the records type 'ER'
total, but I only want the number of rows that are in that state currently. By date it is not possible to have the same record, then I thought that a better way would be for the MAX (id) and with that I can get the number of rows, but I do not know how to do it.
In this SQLFiddle I have a table with more data
I thank you in advance for any help.