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