合并3个表而不使用连接但需要分组

时间:2014-07-31 04:35:34

标签: sql

我有3个不同的表,如下所示:

| DataEntry:                   |  | OOW:               |  | ContractExpired:   |
| +----+-----------------------|  | +----+-------------|  | +----+-------------|
| Country | valid  | invalid   |  | Country | Warranty |  | Country | Expired  |
| AU      | 1      | 2         |  | AU      | 1        |  | AU      | 1        |
| CN      | 22     | 3         |  | CN      | 1        |  | CN      | 1        |
| JP      | 1      | 1         |  | +----+-------------|  | +----+-------------|
| KR      | 1      | 1         |
| SG      | 1      | 1         |
| +----+-----------------------|

我写的查询是这样的:

SELECT Country,
(SELECT SUM(CASE WHEN CallType = 'valid' THEN 1 ELSE Null END) AS [valid] FROM DataEntry AS DataEntry_1) AS [valid],
(SELECT SUM(CASE WHEN CallType = 'Invalid' THEN 1 ELSE Null END) AS [Invalid] FROM DataEntry AS DataEntry_2) AS [Invalid],
(SELECT COUNT(CallType) AS [Warranty] FROM OOW) AS [Warranty],
(SELECT COUNT(CallType) AS [Expired] FROM ContractExpired) AS [Expired]

结果返回如下所示:

| valid | invalid | warranty | Expired |
| 26    | 8       | 2        | 2       |

我想通过分组到国家/地区进行一些更改: (SAMPLE)

| Country | valid | invalid | warranty | Expired |
| AU      | 1     | 2       | 1        | 1       |
| CN      | 22    | 3       | 1        | 1       |
| JP      | 1     | 1       | null     | null    |
| KR      | 1     | 1       | null     | null    |
| SG      | 1     | 1       | null     | null    |

我尝试了组,但查询返回了错误的结果:

SELECT Country,
(SELECT SUM(CASE WHEN CallType = 'valid' THEN 1 ELSE Null END) AS [valid] FROM DataEntry AS DataEntry_1) AS [valid],
(SELECT SUM(CASE WHEN CallType = 'Invalid' THEN 1 ELSE Null END) AS [Invalid] FROM DataEntry AS DataEntry_2) AS [Invalid],
(SELECT COUNT(CallType) AS [Warranty] FROM OOW) AS [Warranty],
(SELECT COUNT(CallType) AS [Expired] FROM ContractExpired) AS [Expired]
FROM DataEntry AS DataEntry_3
Group by Country

结果:

| Country | valid | invalid | warranty | Expired |
| AU      | 26    | 8       | 2        | 2       |
| CN      | 26    | 8       | 2        | 2       |
| JP      | 26    | 8       | 2        | 2       |
| KR      | 26    | 8       | 2        | 2       |
| SG      | 26    | 8       | 2        | 2       |

请提供一些指导来解决这个问题。

4 个答案:

答案 0 :(得分:0)

你的查询不应该像下面那样。这可能不是确切的查询,但会让您了解如何使其工作。

SELECT de.Country,
SUM(CASE WHEN de.CallType = 'valid' THEN 1 ELSE Null END) AS [valid],
SUM(CASE WHEN de.CallType = 'Invalid' THEN 1 ELSE Null END) AS [Invalid],
FROM DataEntry de
join
(
select COUNT(CallType) AS [Warranty] 
FROM OOW
group by country
) tab on de.country = tab.country
join
(
SELECT COUNT(CallType) AS [Expired] 
FROM ContractExpired
group by country
) tab1 on de.country = tab1.country
Group by de.Country

答案 1 :(得分:0)

您需要在子查询中执行分组,然后使用外部联接按国家/地区代码将聚合数据连接在一起(因为并非所有国家/地区都在OOW和ContractExpired表中都有条目。请注意,这只会向国家/地区显示在DataEntry表中至少有一个有效或无效的记录。

SELECT [countryDataEntry].Country, [countryDataEntry].[valid], [countryDataEntry].[Invalid], Warranty, Expired
from ((SELECT Country
            ,SUM(CASE WHEN CallType = 'valid' THEN 1 ELSE Null END) AS [valid]
            ,SUM(CASE WHEN CallType = 'Invalid' THEN 1 ELSE Null END) as [Invalid] 
       FROM DataEntry AS DataEntry_1 GROUP BY Country
      ) AS [countryDataEntry]
    LEFT OUTER JOIN (SELECT Country, COUNT(CallType) AS [Warranty] FROM OOW GROUP BY Country) AS [Warranty] ON [countryDataEntry].Country = [Warranty].Country)
    LEFT OUTER JOIN (SELECT Country, COUNT(CallType) AS [Expired] FROM ContractExpired GROUP BY Country) AS [Expired] ON [countryDataEntry].Country = [Expired].Country

答案 2 :(得分:0)

试试这个:

SELECT * FROM DataEntry DE
LEFT OUTER join OOW ON
OOW.Country = DE.Country
LEFT OUTER JOIN ContractExpired CE ON
CE.Country = DE.Country

以下sqlfiddle仅在3天内有效,如果您想查看带有数据的示例: http://sqlfiddle.com/#!3/539bc/5

答案 3 :(得分:0)

如果你真的想在没有JOIN的情况下这样做,你可以,而且非常简单。您必须使用SUBSELECT,并使用WHERE子句过滤它们:

SELECT de.Country
  SUM(CASE WHEN de.CallType = 'valid'   THEN 1 ELSE Null END) AS [valid],
  SUM(CASE WHEN de.CallType = 'Invalid' THEN 1 ELSE Null END) AS [Invalid],
  (SELECT COUNT(CallType)
    FROM OOW o
    WHERE o.Country=de.Country) AS [Warranty],
  (SELECT COUNT(CallType)
    FROM ContractExpired ce
    WHERE ce.Country=de.Country) AS [Expired]
FROM DataEntry de
GROUP BY de.Country

SQLFiddle here

此SQL假定您具有以下表结构,该结构与您的SQL匹配,但不是您给我们的结构:

DataEntry(
  Country  CHAR,
  Calltype CHAR);

OOW(
  Country  CHAR,
  Calltype CHAR);

CREATE TABLE ContractExpired(
  Country  CHAR,
  Calltype CHAR);

确保所有三个表上的Country都有索引。但对于此处发布的任何答案,使用JOIN或SUBSELECTs,这是一个很好的建议。