简体   繁体   English

从子查询返回列数

[英]Returning count of column from subquery

I have three tables: 我有三个表:

  1. task (id, task_name, event_type_id) 任务(ID,任务名称,事件类型ID)
  2. task_assignment (id, task_id, assignee_id,assignor_id, status) task_assignment(id,task_id,assignee_id,assignor_id,状态)
  3. event_type 事件类型

There is a strange requirement: 有一个奇怪的要求:

I want to all the tasks_assignments grouped by their event_type with the count of tasks in each event_type. 我想将所有task_assignments按其event_type与每个event_type中的任务计数进行分组。

The problem is I want to get all the event types but count only for incomplete tasks. 问题是我想获取所有事件类型,但仅计入未完成的任务。

Here is my query: 这是我的查询:

select *,count(t.id) as total
from event_type et,
  task t,
  task_assignment ta
where et.id = t.event_type_id
  and ta.task_id = t.id
  AND ta.assignee_id=" . $user_id . "
  AND ta.status!='Rejected'
group by t.event_type_id
order by total desc limit " . $limit . " offset ".$offset

What should I do? 我该怎么办?

Try this (you can call it a "conditional count" - a very handy technique for this purpose): 尝试一下(您可以将其称为“条件计数”-为此非常方便的技术):

select t.event_type_id,
    count(t.id) as total, 
    sum(case when ta.status = 'Incomplete' then 1 else 0 end) as total_incomplete
from event_type et,
  task t,
  task_assignment ta
where et.id = t.event_type_id
  and ta.task_id = t.id
  AND ta.assignee_id=" . $user_id . "
  AND ta.status!='Rejected'
group by t.event_type_id
order by total desc limit " . $limit . " offset ".$offset

Oh, and you can only select fields without aggregates (like count() and sum()) if you also group by them, so I changed your select * to select t.event_type_id . 哦,如果您也group by它们进行group by则只能选择没有聚合的字段(例如count()和sum()),因此我将select *更改为select t.event_type_id You can modify this to include whichever columns you need. 您可以修改它以包括所需的任何列。

Per your comments, you could remove the filter from your WHERE clause, and do more with your "conditional counts", with whatever variations you need to fit your particular needs: 根据您的评论,您可以从WHERE子句中删除过滤器,并通过“条件计数”来做更多的事情,并根据自己的特殊需要进行各种更改:

select t.event_type_id,
    count(*) as total_events, 
    sum(case when ta.status = 'Incomplete' then 1 else 0 end) as total_incomplete,
    sum(case when ta.status = 'Incomplete' 
             and ta.status != 'Rejected' then 1 else 0 end) as total_incomplete_not_rejected
    sum(case when ta.status != 'Rejected' then 1 else 0 end) as total_not_rejected
from event_type et,
  task t,
  task_assignment ta
where et.id = t.event_type_id
  and ta.task_id = t.id
  AND ta.assignee_id=" . $user_id . "
group by t.event_type_id
order by total desc limit " . $limit . " offset ".$offset

The main idea is to use SUM and conditions to get the counts for any combination of conditions - this saves you from having to do subqueries, self-joins, multiple queries, etc. Hope it helps. 主要思想是使用SUM和条件获取条件的任意组合的计数-这使您不必进行子查询,自联接,多个查询等操作。希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM