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