1

I've read close to a dozen similar issues on this site, but I don't think my specific case was covered anywhere. I'm not a dba, this is just a side-project I was voluntold for because I knew more SQL than anyone else here, so I won't take it personally if somebody tells me this query is a disaster. :-)

This is my query:

SELECT COUNT(a.issue_type_id) as DistinctIssues, b.issue_type_name, c.subissue_type_name, null AS DistinctTickets
FROM [somedb].[dbo].[wh_task] AS a
INNER JOIN [somedb].[dbo].[wh_issue_type] AS b ON a.issue_type_id = b.issue_type_id
INNER JOIN [somedb].[dbo].[wh_subissue_type] AS c ON a.subissue_type_id = c.subissue_type_id
WHERE a.[create_time] between '11/01/12' AND '01/31/13'
AND a.[account_id] = 123456
AND a.[account_contact_id] is not null
GROUP BY b.issue_type_name, c.subissue_type_name  
UNION
SELECT null as DistinctIssues, b.issue_type_name, c.subissue_type_name, COUNT(a.issue_type_id) as DistinctTickets
FROM [somedb].[dbo].[wh_task] AS a
INNER JOIN [somedb].[dbo].[wh_issue_type] AS b ON a.issue_type_id = b.issue_type_id
INNER JOIN [somedb].[dbo].[wh_subissue_type] AS c ON a.subissue_type_id = c.subissue_type_id
WHERE a.[create_time] between '11/01/12' AND '01/31/13'
AND a.[account_id] = 123456
GROUP BY b.issue_type_name, c.subissue_type_name  

And the results look like this.

DistinctIssues  issue_type_name   subissue_type_name   DistinctTickets
NULL            Storage           EMC                  45
NULL            Storage           HP                   2
NULL            Symantec          Anti Virus           1
NULL            Symantec          Backup Exec          4
NULL            Virtualization    Environmental        1
NULL            Virtualization    Network              5
1               Microsoft         Server 2003          NULL
1               Microsoft         Windows 7            NULL
1               Network           Performance          NULL
1               Virtualization    Environmental        NULL
2               Exchange          Database             NULL

I bet you can guess what I'm trying to do from here. I'm grouping by the two issue types, and I want the count of all tickets, as well as the count of a subset of those tickets. My database is a MS SQL server (2008, I believe). I only have access to views, for what that's worth.

4

2 回答 2

1

两个查询都以相同的条件连接相同的表,根据相同的条件对结果进行分组,甚至计算相同的列。唯一的区别似乎是其中一个查询在 WHERE 子句中使用了附加条件。

在这种情况下,您可以将它们组合成一个查询,对查询返回的值使用条件聚合以及附加条件 ( DistinctIssues):

SELECT
  COUNT(CASE WHEN a.[account_contact_id] is not null THEN a.issue_type_id END) as DistinctIssues,
  b.issue_type_name,
  c.subissue_type_name,
  COUNT(a.issue_type_id) as DistinctTickets
FROM [somedb].[dbo].[wh_task] AS a
INNER JOIN [somedb].[dbo].[wh_issue_type] AS b
  ON a.issue_type_id = b.issue_type_id
INNER JOIN [somedb].[dbo].[wh_subissue_type] AS c
  ON a.subissue_type_id = c.subissue_type_id
WHERE a.[create_time] between '11/01/12' AND '01/31/13'
  AND a.[account_id] = 123456
GROUP BY b.issue_type_name, c.subissue_type_name
;
于 2013-02-05T00:18:37.523 回答
0

很难确定最好的方法是缺少一些信息 - 你的意思是这样的:

WITH Issues AS
(
  SELECT COUNT(a.issue_type_id) as DistinctIssues, b.issue_type_name, c.subissue_type_name
  FROM [somedb].[dbo].[wh_task] AS a
  INNER JOIN [somedb].[dbo].[wh_issue_type] AS b ON a.issue_type_id = b.issue_type_id
  INNER JOIN [somedb].[dbo].[wh_subissue_type] AS c ON a.subissue_type_id = c.subissue_type_id
  WHERE a.[create_time] between '11/01/12' AND '01/31/13'
  AND a.[account_id] = 123456
  AND a.[account_contact_id] is not null
  GROUP BY b.issue_type_name, c.subissue_type_name
),
Tickets AS
(
  SELECT b.issue_type_name, c.subissue_type_name, COUNT(a.issue_type_id) as DistinctTickets
  FROM [somedb].[dbo].[wh_task] AS a
  INNER JOIN [somedb].[dbo].[wh_issue_type] AS b ON a.issue_type_id = b.issue_type_id
  INNER JOIN [somedb].[dbo].[wh_subissue_type] AS c ON a.subissue_type_id = c.subissue_type_id
  WHERE a.[create_time] between '11/01/12' AND '01/31/13'
  AND a.[account_id] = 123456
  GROUP BY b.issue_type_name, c.subissue_type_name
)
SELECT i.DistinctIssues
  , COALESCE(i.issue_type_name, t.issue_type_name) AS issue_type_name
  , COALESCE(i.subissue_type_name, t.subissue_type_name) AS subissue_type_name
  , t.DistinctTickets
FROM Issues i
  FULL JOIN Tickets t ON i.issue_type_name = i.issue_type_name
    AND i.subissue_type_name = t.subissue_type_name

这会为存在的每个组合提供问题和票证。

如果这不是必需的,也许您还可以提供示例数据集和所需的输出?

于 2013-02-04T22:44:01.490 回答