将另一个不同值的值相加

时间:2012-08-01 15:02:29

标签: mysql

在做了一些连接之后,我得到了一个非分组的结果/视图,如:

id | from                | to                  | who    | group | ...
 1 | 2012-01-01 12:00:00 | 2012-01-01 14:00:00 | adam   | sales
 2 | 2012-01-01 12:00:00 | 2012-01-01 15:00:00 | bertil | sales
 2 | 2012-01-01 12:00:00 | 2012-01-01 15:00:00 | bertil | admin
...

结果将按日期(从)

分组

但我也对时间长度(UNIX_TIMESTAMP(to) - UNIX_TIMESTAMP(from))/3600感兴趣 我想要按日分组的总和,但我只希望每个id计算一次

我可以这样做:
SUM((UNIX_TIMESTAMP(to) - UNIX_TIMESTAMP(from))/3600 DISTINCT id)
或者可以使用变量和IF()?
或者我必须通过按ID分组的大查询执行子查询,然后按日期分组外部查询?

更新1 澄清您希望看到的答案 上面的结果是人们工作的时间,以及他们所属的团体, 过滤掉目前感兴趣的群体后, 我想知道那天花了多少时间, 示例数据:adam为2小时,bertil为3小时= 5小时, 但bertil是2个有趣组的成员,所以他的时间显示两次

更新2 提供更多数据
以上是对此查询的概括性尝试

SELECT
   worktimes.date AS 'Datum',
   COUNT(DISTINCT employments.citizen_id) AS 'Säljare',
   SUM(UNIX_TIMESTAMP(TIMESTAMP(worktimes.date, worktimes.death)) - UNIX_TIMESTAMP(TIMESTAMP(worktimes.date, worktimes.birth))) AS 'Mantid',
   COUNT(DISTINCT agreements.agreement_id) AS 'Avtal',
   COUNT(DISTINCT IF(agreement_status.status_id = 57, agreements.agreement_id, NULL)) AS 'Godkända',
   COUNT(DISTINCT IF(agreement_status.status_id = 3, agreements.agreement_id, NULL)) AS 'Ånger',
   COUNT(DISTINCT IF(agreement_status.status_id = 4, agreements.agreement_id, NULL)) AS 'Makuleringar',
   COUNT(DISTINCT IF(agreement_status.status_id IS NULL, agreements.agreement_id, NULL)) AS 'Övrigt'
FROM worktimes
LEFT JOIN employments USING (employment_id)
LEFT JOIN membership_cache ON (
   membership_cache.target_type = 34 AND
   membership_cache.target_id IN (136, 138) AND
   membership_cache.member_type = 11 AND
   membership_cache.member_id = employments.citizen_id AND
   DATE(membership_cache.birth) <= worktimes.date AND
   (membership_cache.death IS NULL OR worktimes.date < DATE(membership_cache.death))
   )
LEFT JOIN agreements ON (
   agreements.citizen_id = employments.citizen_id AND
   agreements.project_id = 20 AND
   agreements.date >= 20110101 AND
   DATE(agreements.date) = worktimes.date
   )
LEFT JOIN agreement_status ON (
   agreements.agreement_id = agreement_status.agreement_id AND
   agreement_status.value = 1 AND
   agreement_status.death IS NULL AND
   agreement_status.status_id IN (3, 4, 57)
   )
WHERE
   worktimes.death IS NOT NULL AND
   membership_cache.member_id IS NOT NULL AND
   worktimes.date >= 20110101
GROUP BY DATE(agreements.date)

以上查询的mysql EXPLAIN

+----+-------------+------------------+-------+------------------------------------------------------+------------------------+---------+-------------------------------+------+-----------------------------------------------------------+
| id | select_type | table            | type  | possible_keys                                        | key                    | key_len | ref                           | rows | Extra                                                     |
+----+-------------+------------------+-------+------------------------------------------------------+------------------------+---------+-------------------------------+------+-----------------------------------------------------------+
|  1 | SIMPLE      | membership_cache | range | target,member                                        | target                 | 10      | NULL                          |   85 | Using where; Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | employments      | ref   | employment_id,citizen                                | citizen                | 8       | db.membership_cache.member_id |    1 | Using where; Using index                                  |
|  1 | SIMPLE      | worktimes        | ref   | employment,date,death                                | employment             | 8       | db.employments.employment_id  |   34 | Using where                                               |
|  1 | SIMPLE      | agreements       | ref   | project,date,project_customer,agreements_per_citizen | agreements_per_citizen | 8       | db.employments.citizen_id     |   36 |                                                           |
|  1 | SIMPLE      | agreement_status | ref   | agreement,status_birth,status_death                  | agreement              | 8       | db.agreements.agreement_id    |    1 |                                                           |
+----+-------------+------------------+-------+------------------------------------------------------+------------------------+---------+-------------------------------+------+-----------------------------------------------------------+

问题是SUM(UNIX_TIMESTAMP(TIMESTAMP(worktimes.date, worktimes.death)) - UNIX_TIMESTAMP(TIMESTAMP(worktimes.date, worktimes.birth))) AS 'Mantid'乘以表中匹配行的数量:membership_cache,agreement和agreement_status

0 个答案:

没有答案