1

我有一个产生以下结果的查询:

表格1:

degree_code  occupation_code  degree_completions  degree_level
1.0000       20-2021          10                  1
1.0000       20-2022          10                  1
1.1051       52-2095          2                   3
1.1051       52-2095          41                  2
1.5010       15-1100          2                   3

我有另一个小的查找表,它将 degree_level 与我需要使用的自定义类别相关联:

表2

degree_level  degree_level_recode
1             AADEGREE
2             AADEGREE
3             BACHDEGREE

我想构建第一个查询的输出以报告以下格式:

degree_code  occupation_code  degree_completions  degree_level AADEGREE BACHDEGREE
1.0000       20-2021          10                  1            10       0
1.0000       20-2022          10                  1            10       0
1.1051       52-2095          2                   3            3        0
1.1051       52-2095          41                  2            0        41
1.5010       15-1100          2                   3            2        1

基本上,在原始查询中创建新的临时重新编码字段,当它们与 degree_level_recode 匹配时报告它们下面的 degree_completions,如果不匹配则输入 0。这是高度简化的;我将针对查询中每个元素中的其他字段对重新编码字段执行操作。

我已经展示了 degree_completions 字段以供参考,但出于明显的冗余原因,我将在最终查询中省略它。

我想我需要使用 CASE 语句或类似的东西进行比较检查,但我是 SQL 新手。

编辑:

参考下面 Cha 的回答,采用修改后的 table1 输出(映射重新编码后):

degree_code  degree_level  degree_completions  degree_level_recode
01.0601      2             11                  LESSCOLL
01.0601      3             22                  AADEGR
01.0605      2             3                   LESSCOLL

考虑这段代码(table2 在编辑上方引用):

SELECT degree_code
    ,degree_level
    ,[LESSCOL] AS LESSCOL
    ,[AADEGR] AS AADEGR
    ,[BACHDEGR] AS BACHDEGR
    ,[MADEGR] AS MADEGR
    ,[DOCDEGR] AS DOCDEGR
FROM 
    (
    SELECT degree_code
        ,table1.degree_level
        ,degree_level_recode
        ,degree_code
     FROM table1
          ,table2 
     WHERE table1.degree_level = table2.degree_code
     ) AS p
     PIVOT
         (
         SUM (degree_completions)
         FOR degree_level_recode IN ([LESSCOL], [AADEGR], . . .)
         ) AS pvt

这会产生以下结果:

degree_code  degree_level  LESSCOL  AADEGR  BACHDEGR  MADEGR  DOCDEG  
01.0601      2             NULL     NULL    NULL      NULL    NULL
01.0601      3             NULL     22      NULL      NULL    NULL
01.0505      2             NULL     NULL    NULL      NULL    NULL

我试图达到:

degree_code  degree_level  LESSCOL  AADEGR  BACHDEGR  MADEGR  DOCDEG  
01.0601      2             11       NULL    NULL      NULL    NULL
01.0601      3             NULL     22      NULL      NULL    NULL
01.0505      2             3        NULL    NULL      NULL    NULL

此外,将 NULL 替换为 0。

4

1 回答 1

1

这就是您所追求的(假设:您的第一个表称为#temp1,您的第二个表称为#temp2):

SELECT *
FROM
#temp1,
(SELECT degree_level, [AADEGREE] as col1, [BACHDEGREE] as col2
FROM 
(SELECT degree_completions, #temp1.degree_level, degree_level_recode
FROM #temp1, #temp2 WHERE #temp1.degree_level = #temp2.degree_level) AS p
PIVOT
(
SUM (degree_completions)
FOR degree_level_recode IN
([AADEGREE], [BACHDEGREE])
) AS pvt
) as V
WHERE #temp1.degree_level = V.degree_level
ORDER BY 1
于 2012-10-19T00:52:26.233 回答