I have a db with three tables:
我有一个包含三个表的数据库:
Client: ID---Name
Organization: ID---Descriptio---Client_ID
Ticket: ID---Organization_ID---Status
I can list all the ticket for a specific client with a simple query:
我可以使用简单的查询列出特定客户端的所有票证:
SELECT *
FROM Organization JOIN
Client ON organization.client_id=client.id
Ticket ON ticket.organization_ID=organization.id
Tickets can have a status open or close so I'd like to run a query thtat will return:
门票可以打开或关闭状态,所以我想运行查询thtat将返回:
- foreach Client.id;
- Foreach Client.id;
- foreach Organization.id;
- foreach Organization.id;
- the total number of tickets;
- 门票总数;
- the count of how many open ones;
- 有多少开放的人数;
- the count of how many close ones;
- 有多少亲密的人数;
I can't find a suitable query to run that will give me in the same row both how many open and how many close and the total number. (The total number is to check that each ticket has a status). I need this query to double-check db integrity and fix any issue from the legacy system. I know how to build it in steps but it is a massive amount of data and would be the best to do it in one shot
我找不到一个合适的查询来运行,这将给我在同一行中打开多少以及关闭多少和总数。 (总数是检查每张票都有状态)。我需要此查询来仔细检查数据库完整性并修复遗留系统中的任何问题。我知道如何按步骤构建它,但它是大量的数据,并且最好一次性完成
Any idea?
任何想法?
EDIT: the point is to make two conditional count on the same field in the same query. The question marked as duplicated does not address this specific issue
编辑:重点是在同一查询中对同一个字段进行两次条件计数。标记为重复的问题并未解决此特定问题
1 个解决方案
#1
0
you can use something like count(*),sum(open=1),sum(open=0) to get total,open and closed tickets. Change the open=... to match the column definition
您可以使用诸如count(*),sum(open = 1),sum(open = 0)之类的东西来获得总票数,开票票数和已结票票数。更改open = ...以匹配列定义
#1
0
you can use something like count(*),sum(open=1),sum(open=0) to get total,open and closed tickets. Change the open=... to match the column definition
您可以使用诸如count(*),sum(open = 1),sum(open = 0)之类的东西来获得总票数,开票票数和已结票票数。更改open = ...以匹配列定义