Unpivot:列模糊定义

时间:2014-09-15 11:28:37

标签: sql oracle

在下面的语句中运行时我得到了#34;列模糊定义" ..

请帮帮我..

SELECT *
FROM
    (


  SELECT  activity_type,
      COUNT(decode(CASE WHEN activity_type = 'K' then 1 when activity_type='BH' then 1 END,1,1))  Holiday,
  COUNT(decode(CASE WHEN activity_type = 'SL' then 1 when activity_type='2' then 1 END,1,1)) Leave,
  COUNT(CASE WHEN activity_type = 'AL'  then 1 ELSE NULL END) Annual,
  COUNT(CASE WHEN activity_type = 'OL'  then 1 ELSE NULL END) Other_no,
  COUNT(CASE WHEN activity_type = 'IM'  then 1 ELSE NULL END) InternalMeeting,
  COUNT(CASE WHEN activity_type = 'TR'  then 1 ELSE NULL END) Training,
  COUNT(CASE WHEN activity_type = 'ISM'  then 1 ELSE NULL END) Office,
  COUNT(decode(CASE WHEN activity_type = 'CS' then 1 when activity_type='10' then 1 when activity_type='SAV'then 1 END,1,1,1)) ExternalMeeting,
  COUNT(CASE WHEN activity_type = '9'  then 1 ELSE NULL END) Symposium,
  COUNT(CASE WHEN activity_type = '12'  then 1 ELSE NULL END) Transit,
  COUNT(CASE WHEN activity_type = 'H'  then 1 ELSE NULL END) Result,
  COUNT(CASE WHEN activity_type = '10'  then 1 ELSE NULL END) CME
    FROM  planner_activity pa
    WHERE tenant_id=500020
       GROUP BY activity_type
     )
     UNPIVOT
     (
  monthly_count
       FOR activity_type IN (HOLIDAY,LEAVE,ANNUAL,OTHER_NO,INTERNALMEETING,TRAINING,OFFICE,EXTERNALMEETING,SYMPOSIUM,TRANSIT,RESULT,CME)
  )

我想做结果的总和..请帮助我..

输出应该是:

Holiday 1
Leave   1
Anuual  2
Other_no 1
Noof days 5

非常感谢你的帮助..

... Sunitha

2 个答案:

答案 0 :(得分:1)

尝试更改为:

SELECT *
FROM
    (


  SELECT  activity_type,
      COUNT(decode(CASE WHEN activity_type = 'K' then 1 when activity_type='BH' then 1 END,1,1))  Holiday,
  COUNT(decode(CASE WHEN activity_type = 'SL' then 1 when activity_type='2' then 1 END,1,1)) Leave,
  COUNT(CASE WHEN activity_type = 'AL'  then 1 ELSE NULL END) Annual,
  COUNT(CASE WHEN activity_type = 'OL'  then 1 ELSE NULL END) Other_no,
  COUNT(CASE WHEN activity_type = 'IM'  then 1 ELSE NULL END) InternalMeeting,
  COUNT(CASE WHEN activity_type = 'TR'  then 1 ELSE NULL END) Training,
  COUNT(CASE WHEN activity_type = 'ISM'  then 1 ELSE NULL END) Office,
  COUNT(decode(CASE WHEN activity_type = 'CS' then 1 when activity_type='10' then 1 when activity_type='SAV'then 1 END,1,1,1)) ExternalMeeting,
  COUNT(CASE WHEN activity_type = '9'  then 1 ELSE NULL END) Symposium,
  COUNT(CASE WHEN activity_type = '12'  then 1 ELSE NULL END) Transit,
  COUNT(CASE WHEN activity_type = 'H'  then 1 ELSE NULL END) Result,
  COUNT(CASE WHEN activity_type = '10'  then 1 ELSE NULL END) CME
    FROM  planner_activity pa
    WHERE tenant_id=500020
       GROUP BY activity_type
     )
     UNPIVOT
     (
  monthly_count
       FOR new_activity_type IN (HOLIDAY,LEAVE,ANNUAL,OTHER_NO,INTERNALMEETING,TRAINING,OFFICE,EXTERNALMEETING,SYMPOSIUM,TRANSIT,RESULT,CME)
  )

activity_type在此查询中使用了两次,可能导致问题。

答案 1 :(得分:0)

我不确定你为什么会收到这个错误,但为什么不为此做一个group by呢?像这样:

SELECT grp, count(*)
FROM (SELECT (CASE WHEN activity_type = 'SL' then 'Leave'
                   WHEN activity_type = 'AL' then 'Annual'
                   WHEN activity_type = 'OL'  then 'Other_no'
                   WHEN activity_type = 'IM'  then 'InternalMeeting'
                   WHEN activity_type = 'TR'  then 'Training'
                   WHEN activity_type = 'ISM'  then 'Office'
                   WHEN activity_type IN ('CS', '10', 'SAV') then 'ExternalMeeting'
                   WHEN activity_type = '9' then 'Symposium'
                   WHEN activity_type = '12'  then 'Transit'
                   WHEN activity_type = 'H'  then 'Result'
                   WHEN activity_type = '10'  then 'CME'
              END) as grp, pa.*
      FROM planner_activity pa
      WHERE tenant_id = 500020
     ) t
GROUP BY grp;