2

我有两张表:
表 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 
4

3 回答 3

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
于 2013-09-11T08:15:04.720 回答
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 以使结果与您相同。

我认为第二个作为查询没有用,因为您丢失了例如有 2xUK Staff和 2x的信息US Admin Staff

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

于 2013-09-11T08:19:18.493 回答
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 演示

于 2013-09-11T08:20:54.313 回答