计算SQL Server 2008中每个国家/地区的人数

时间:2013-09-11 07:52:02

标签: sql sql-server sql-server-2008

我有两张桌子:
表1:

------------------------------
|Country    |Emp Level  |Name|
|-----------|-----------|-----
|UK         |Staff      |A
|US         |Admin Staff|B
|CA         |Staff      |C
|NL         |Admin Staff|D
|MN         |Intern     |E
|IN         |Staff      |F
|UK         |Staff      |G
|US         |Admin Staff|H
------------------------------

表2:

---------------------------------------------
|Level Guid |Level name |Country code       |
|-----------|-----------|-------------------|
|101        |Admin Staff|UK                 |
|102        |Admin Staff|US                 |
|103        |Admin Staff|Global             |
|105        |Staff      |UK                 |
|106        |Staff      |US                 |
|107        |Staff      |Global             |
|108        |Intern     |UK                 |
|109        |Intern     |US                 |
|110        |Intern     |Global             |
---------------------------------------------

结果应该是:

|Level ID   |Level      |Country| Count     |
|105        |Staff      |UK     | 2         |
|102        |Admin Staff|US     | 2         |
|107        |Staff      |CA     | 1         |
|103        |Admin Staff|NL     | 1         |
|110        |Intern     |MN     | 1         |
|107        |Staff      |IN     | 1         |

在表2中,除美国以外的任何国家/地区都被视为全球。请注意,即使在表2中,除了美国,英国以外的所有国家都是全球性的,所以这些国家都是单独计算的。我已经尝试了查询,但它给了我错误More than 1 value returned in subquery

SELECT country, 
       emp - levels, 
       Count(emp - levels) count, 
       "LevelID"= CASE 
                    WHEN (SELECT level_guid 
                          FROM   table_2 T 
                          WHERE  u.emp - levels = T.level_name 
                                 AND u.country IN ( 'UK', 'US' )) IS NOT NULL 
                  THEN 
                    (SELECT level_guid 
                     FROM   table_2 T 
                     WHERE  u.emp - levels = T.level_name 
                            AND u.country NOT IN ( 'UK', 'US' )) 
                    ELSE (SELECT level_guid 
                          FROM   [DPN_Reporting].dbo.taxonomytermset T 
                          WHERE  u.emp_levels = T.level_name 
                                 AND T.level_name = 'Global') 
                  END 
FROM   table_1 u 
WHERE  emp - levels IS NOT NULL 
       AND emp - levels != '' 
GROUP  BY emp - levels, 
          country 
ORDER  BY Count(emp - levels) DESC 

3 个答案:

答案 0 :(得分:1)

试试这个

CASE 
    WHEN EXISTS(SELECT level_guid 
                FROM   table_2 T 
                WHERE  u.emp - levels = T.level_name 
                AND u.country IN ( 'UK', 'US' )) 
   THEN (SELECT MAX(level_guid)
        FROM   table_2 T 
        WHERE  u.emp - levels = T.level_name 
        AND u.country NOT IN ( 'UK', 'US' )) 
   ELSE (SELECT MAX(level_guid)
        FROM   [DPN_Reporting].dbo.taxonomytermset T 
        WHERE  u.emp_levels = T.level_name 
        AND T.level_name = 'Global') 
END

答案 1 :(得分:1)

这里有两个查询:

SELECT T2.[Level Guid] [Level ID], T1.[Emp Level] Level, T1.Country, COUNT(*) Count 
    FROM Table1 T1
    INNER JOIN Table2 T2 ON T1.[Emp Level] = T2.[Level name] 
        AND CASE T1.Country WHEN 'UK' THEN 'UK' WHEN 'US' THEN 'US' ELSE 'Global' END = T2.[Country code]
    GROUP BY T2.[Level Guid], T1.[Emp Level], T1.Country

SQLFiddle:http://sqlfiddle.com/#!3/18e8f/8

SELECT DISTINCT T2.[Level Guid] [Level ID], T1.[Emp Level] Level, T1.Country, 
    (SELECT COUNT(*) FROM Table1 T3 WHERE T1.Country = T3.Country) Count
    FROM Table1 T1
    INNER JOIN Table2 T2 ON T1.[Emp Level] = T2.[Level name] 
        AND CASE T1.Country WHEN 'UK' THEN 'UK' WHEN 'US' THEN 'US' ELSE 'Global' END = T2.[Country code]

SQLFiddle:http://sqlfiddle.com/#!3/18e8f/7

Count中的第一个将给出具有相同T2.[Level Guid], T1.[Emp Level], T1.Country的人数。

Count中的第二个将给出相同Country的人数。请注意,我添加了DISTINCT以使结果与您相同。

我认为第二个作为查询是无用的,因为您丢失了例如2x UK Staff和2x US Admin Staff的信息。

作为建议,请不要在列名中添加空格!

答案 2 :(得分:1)

这样的事可能适合你:

SELECT  [Level Guid], [Level name], Country , COUNT(*) [Count]
FROM dbo.table_1 t1
LEFT JOIN dbo.table_2 t2 ON t2.[Country code] = CASE WHEN t1.Country IN ('UK','US') THEN t1.Country ELSE 'Global' END
    AND t1.[Emp Level] = t2.[Level name]
GROUP BY [Level Guid], [Level name], Country
ORDER BY COUNT(*) DESC

<强> SQLFiddle DEMO