SQLite 计数汇总查询

发布于 2024-11-25 17:38:02 字数 1988 浏览 2 评论 0原文

我正在尝试获取一个查询来总结每个员工本周的工作。例如,John Doe 本周总共制作了 12 个工单,其中 4 个是“中断/修复”,4 个是“增强”,另外 4 个是未分类。

这是我到目前为止所得到的:

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  COUNT(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
  COUNT(tickets.category = 'After Hours') AS 'After Hours Tickets',
  COUNT(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
  COUNT(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
  COUNT(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16 00:00:00') AND
  (tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;

这是一个示例结果:

John Doe1 10 10 10 10 10 10

John Doe2 2 2 2 2 2 2

John Doe3 25 24 24 24 24 24

John Doe4 2 2 2 2 2 2

John Doe5 12 10 10 10 10 10

无名氏6 7 7 7 7 7 7

该查询不太像我预期的那样工作,因为所有列的总数相同(已关闭的票证总数,以下列似乎只包含分类的票证。)有帮助吗?

编辑

只是想发布功能代码:

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  COUNT(case tickets.category when 'Maintenance' then 1 else null end) AS 'Maintenance Tickets',
  COUNT(case tickets.category when 'After Hours' then 1 else null end) AS 'After Hours Tickets',
  COUNT(case tickets.category when 'Break Fix' then 1 else null end) AS 'Break Fix Tickets',
  COUNT(case tickets.category when 'Enhancement' then 1 else null end) AS 'Enhancement Tickets',
  COUNT(case tickets.category when '' then 1 else null end) AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16') AND
  (tickets.closed_at <= '2011-07-22')
GROUP BY Name;

I'm trying to get a query to summarize each employees work for the week. For example, John Doe did a total of 12 tickets for the week, 4 of which were Break/Fixes, and 4 were Enhancement, and another 4 were uncategorized.

This is what I have so far:

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  COUNT(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
  COUNT(tickets.category = 'After Hours') AS 'After Hours Tickets',
  COUNT(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
  COUNT(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
  COUNT(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16 00:00:00') AND
  (tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;

Here is a sample result:

John Doe1 10 10 10 10 10 10

John Doe2 2 2 2 2 2 2

John Doe3 25 24 24 24 24 24

John Doe4 2 2 2 2 2 2

John Doe5 12 10 10 10 10 10

John Doe6 7 7 7 7 7 7

This query doesn't quite work as I expected it to as all of the columns have the same total (The total number of tickets closed, the following columns seems to only contain the categorized ones.) Help?

EDIT

Just wanted to post the functional code:

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  COUNT(case tickets.category when 'Maintenance' then 1 else null end) AS 'Maintenance Tickets',
  COUNT(case tickets.category when 'After Hours' then 1 else null end) AS 'After Hours Tickets',
  COUNT(case tickets.category when 'Break Fix' then 1 else null end) AS 'Break Fix Tickets',
  COUNT(case tickets.category when 'Enhancement' then 1 else null end) AS 'Enhancement Tickets',
  COUNT(case tickets.category when '' then 1 else null end) AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16') AND
  (tickets.closed_at <= '2011-07-22')
GROUP BY Name;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-12-02 17:38:02

你可能想像这样使用 COUNT

...
    COUNT(case tickets.category when 'Maintenance' then 1 else null end),
    COUNT(case tickets.category when 'After Hours' then 1 else null end),
...

you may want to use COUNT like this

...
    COUNT(case tickets.category when 'Maintenance' then 1 else null end),
    COUNT(case tickets.category when 'After Hours' then 1 else null end),
...
只有影子陪我不离不弃 2024-12-02 17:38:02

在我看来,您不能在 GROUP BY 子句中使用别名。您的用户没有可以用来区分他们的 ID 吗?

如果要与条件进行比较,则必须使用 SUM 而不是 COUNT。

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  SUM(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
  SUM(tickets.category = 'After Hours') AS 'After Hours Tickets',
  SUM(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
  SUM(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
  SUM(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16 00:00:00') AND
  (tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;

It seems to me you cannot use an alias in the GROUP BY clause. Don't your users have an ID you could use to differenciate them?

And you must use SUM instead of COUNT if you want to count compared with a condition.

SELECT (users.first_name || ' ' || users.last_name) AS Name,
  COUNT(tickets.id) AS 'Number of Tickets Closed',
  SUM(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
  SUM(tickets.category = 'After Hours') AS 'After Hours Tickets',
  SUM(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
  SUM(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
  SUM(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
  (tickets.closed_at >= '2011-07-16 00:00:00') AND
  (tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文