SQLite select and count from another table with like

karmel :

I have two tables as follow:

TABLE PERSONS

           ------------------------------
           idpers | name      | firstname
           ------------------------------
           1      | John      | Miad
           2      | Eddy      | Rowan
           3      | Phil      | Barzoon
           ...

TABLE TASKS

           -----------------------------------------------------------------------
           id_td  |  date     | task           | protocol
           -----------------------------------------------------------------------
           211    | 13-12-19  | Mount          | 13-12-19•John Miad→13-12-19•Eddy Rowan
           348    | 14-12-19  | Fix and clean  | 14-12-19•Eddy Rowan
           256    | 15-12-19  | Lubricate      | 15-12-19•Phil Barzoon
           265    | 15-12-19  | First run      | 15-12-19•Phil Barzoon→15-12-19•John Miad
           ...

I want to create a view DAILY_TOTAL_BY_DOER which calculate for each name the number of tasks done like this

View Ex:

enter code here
           ------------------------------------------------------------------------
              date  | idpers    | name      | firstname | total_tasks 
           ------------------------------------------------------------------------
           13-12-19 | 1         | John      | Miad      |  7    
           13-12-19 | 2         | Eddy      | Rowan     |  3    
           13-12-19 | 3         | Phil      | Barzoon   |  8    
           14-12-19 | 6         | Andreas   | werner    |  5   
           ...

Thank you so much for any help.

forpas :

If the format of the column protocol is always the same as in your sample data, then you can join the tables and group by date and person:

select t.date, p.idpers, p.name, p.firstname, count(*) total_tasks 
from persons p inner join tasks t
on t.protocol  || '→' like '%•' || p.name || ' ' || p.firstname || '→%'
group by t.date, p.idpers, p.name, p.firstname

See the demo.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33391&siteId=1