根据SQLite中的一系列值和得分表按总分进行分组

时间:2013-02-12 03:01:46

标签: sql sqlite

给出一个关于人们何时开始和完成任务的大数据表,例如:

Person  |  Task | Date started | Date ended
---------------------------------------------
A        Cleaning  20-FEB-2012  22-FEB-2012
N        Dishes    20-FEB-2012  24-FEB-2012
Z        Cleaning  21-FEB-2012  23-FEB-2012

和一个分数表,根据每个任务执行的时间分配2,3,4的分数,例如:

|  Task | Days taken | Score
---------------------------
Cleaning      2        2
Cleaning      1.5      3
Cleaning      1        4
Dishes        3        2
Dishes        2.5      3
Dishes        2        4

我如何生成一个查询,该查询给出每个任务的每个人的总分,例如:

Person  |  Task | Overall Score
---------------------------------------------
A        Cleaning  3.1
A        Dishes    2.7
N        Cleaning  3.4

解决方案一直在巧妙地避开我,一些帮助将不胜感激!我目前正在使用SQLite。

1 个答案:

答案 0 :(得分:0)

你的定义有点模糊。但是,以下内容可以帮助您:

select t.person, t.task, sum(s.score)
from tasks t left outer join
     score s
     on t.task = s.task and
        s.daysTaken = t.julianday(dateended) - t.julianday(datestarted)
group by t.person, t.task

处理范围有点困难。您需要获取间隔的两端,然后执行连接:

select t.person, t.task, sum(s.score)
from tasks t left outer join
     (select s.*,
             (select min(days_taken) from score s2 where s2.person = s.person and s2.task = s.task and s2.days_taken > s.days_taken
             ) as nextDays_Taken
      from score s
     ) s
     on t.task = s.task and
        t.julianday(dateended) - t.julianday(datestarted) >= s.days_taken and
        t.julianday(dateended) - t.julianday(datestarted) < nextDays_Taken
group by t.person, t.task