Oracle SQL摘要/数据透视表

时间:2012-09-10 09:40:11

标签: sql oracle pivot case crosstab

我有一个如下所示的数据集:

    ID      Description                     Team
    A       Missing Name; Missing Gender    Team1
    B       Missing Gender                  Team1
    C       Missing Name                    Team2
    D       Missing Name                    Team2

即。每个ID都有一行,带有一个文本列(描述),用于标识ID的任何问题,以及ID所属团队的名称。我已经设法总结如下:

    Team        Missing Gender      Missing Name
    Team1       2                   1
    Team2       0                   2

使用如下所示的case语句:

       SELECT
    "Team",
    sum(case when "DQ Info" like '%Missing Gender%' then 1 ELSE 0 END) as "Missing Gender",
    sum(case when "DQ Info" like '%Missing Name%' then 1 ELSE 0 END) as "Missing Name"
    FROM
    (*)
    GROUP BY "Team"

我希望能够总结这些数据,使其看起来像这样:

    Description         Team1       Team2
    Missing Gender      2           0
    Missing Name        1           2

我知道必须有一种方法可以做到这一点,但我现在正在努力 - 只需在上面的字段中使用“描述”而不是“团队”就会产生如下结果:

    Description                     Team1       Team2
    Missing Name; Missing Gender    1           0
    Missing Gender                  1           0
    Missing Name                    0           2

这不是我所追求的。任何帮助表示赞赏,但我会继续修补,所以如果我设法解决这个问题,我会更新这篇文章。请注意,这只是数据集的一个示例,而不是实际的数据集。

编辑:按照bpgergo的建议在下面查询。完成一些阅读之后,看起来下面会出现短路,即一旦满足内部情况中的第一个标准就终止查询,这意味着它永远不能多次计算一个列。这很遗憾,但我想我必须坚持原始查询。

    SELECT
    "Description",
    sum(case when "Team" like 'Team1' then 1 ELSE 0 END) as "Team1",
    sum(case when "Team" like 'Team2' then 1 ELSE 0 END) as "Team2"
    FROM(SELECT
    "Team",
    case when "DQ Info" like '%[Check Role]%' then '[Check Role]'
    ELSE case when "DQ Info" like '%[Client deceased]%' then '[Client deceased]'
    ELSE case when "DQ Info" like '%[2 Parents not recorded]%' then '[2 Parents not recorded]'
    ELSE case when "DQ Info" like '%[Religion not recorded]%' then '[Religion not recorded]'
    ELSE case when "DQ Info" like '%[1st Language not recorded]%' then '[1st Language not recorded]'
    ELSE case when "DQ Info" like '%[Ethnicity not recorded]%' then '[Ethnicity not recorded]'
    ELSE case when "DQ Info" like '%[No Current Worker]%' then '[No Current Worker]'
    ELSE case when "DQ Info" like '%[No Current Team]%' then '[No Current Team]'
    ELSE case when "DQ Info" like '%[Update Gender]%' then '[Update Gender]'
    ELSE case when "DQ Info" like '%[No Plan]%' then '[No Plan]'
    ELSE case when "DQ Info" like '%[Update Plan]%' then '[Update Plan]'
    END END END END END END END END END END END
     as "Description"
    FROM(
    *
    FROM
    (*)WORKLOAD)DATA)
    GROUP BY "Description"

1 个答案:

答案 0 :(得分:0)

你已经到了一半。尝试这样的内部选择

select 
case when "Description" like '%Missing Gender%' then "Missing_gender" 
ELSE case when "Description" like '%Missing Name%' then "Missing_name" ELSE null END
END as "Description",
Team
from your_table

整个选择将是这样的

SELECT
"Description",
sum(case when "Team" = 'Team1' then 1 ELSE 0 END) as "Team1",
sum(case when "Team" = 'Team2' then 1 ELSE 0 END) as "Team2",
FROM
(
  select 
  case when "Description" like '%Missing Gender%' then "Missing_gender" 
  ELSE case when "Description" like '%Missing Name%' then "Missing_name" ELSE null END
  END as "Description",
  Team
  from your_table
)
GROUP BY "Description"