【问题标题】:SQLite select and count from another table with likeSQLite 从另一个表中选择并计数
【发布时间】:2020-06-19 23:30:08
【问题描述】:

我有两张表如下:

桌人

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

表任务

           -----------------------------------------------------------------------
           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
           ...

我想创建一个视图 DAILY_TOTAL_BY_DOER,它为每个名称计算像这样完成的任务数

查看示例:

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   
           ...

非常感谢您的帮助。

【问题讨论】:

  • 你尝试了什么,出现了什么问题?
  • 嗨,我尝试了 SELECT name, firstname,(SELECT count(case when protocol like '%'+name+'%' then 1 else null end) FROM tasks) 作为 tottd FROM 人员,但没有总数..只是 0

标签: sqlite select count wildcard


【解决方案1】:

如果protocol 列的格式始终与您的示例数据中的格式相同,那么您可以加入表格并按日期和人员分组:

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

请参阅demo

【讨论】:

  • 是的,这就是协议列的结构。
猜你喜欢
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2015-04-10
  • 2018-03-09
相关资源
最近更新 更多