2

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

このようなものがうまくいくかもしれません:

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 に答える
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 に答える