加入两个具有重叠值的SQL查询,以添加第三列

时间:2013-02-04 21:19:55

标签: sql-server

我在这个网站上已经阅读了近十几个类似的问题,但我认为我的具体案例并未涵盖在任何地方。我不是dba,这只是我自愿参与的一个副项目,因为我比其他任何人都知道更多的SQL,所以如果有人告诉我这个查询是一场灾难,我不会亲自接受。 : - )

这是我的疑问:

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  

结果看起来像这样。

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

我打赌你可以从这里猜到我想要做什么。 我按两种问题类型进行分组,我想要所有票证的数量,以及这些票证子集的数量。我的数据库是一个MS SQL服务器(2008年,我相信)。我只能查看视图,这是值得的。

2 个答案:

答案 0 :(得分: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
;

答案 1 :(得分: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

这为现有的每种组合提供了问题和票证。

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