PIVOT IN子句中的动态查询

时间:2014-08-04 07:29:41

标签: oracle oracle11g

我需要在oracle 11g中的pivot查询的In子句中编写动态查询。使用Pivot xml,它是可能的,但我不需要xml。这是代码段。

WITH pivot_data AS (
    select cu.id, u.topic, cu.first_name, cu.last_name, cu.email,
        trunc(cu.REGISTRATION_DATE) Register_Date,
        trunc(min(u.view_date)) First_Visit,
        trunc(max(u.view_date)) Last_Visit,
        nvl(sum(u.user_visits),0) Visits,
        nvl(sum(u.time_in_topic),0) Time_in_Topic,
        ffl.label label, ffv.field_value val
    from ACTIVE_USER_VIEWS_BY_TOPIC u
    LEFT join cl_user cu
        on u.user_id=cu.id
    LEFT JOIN CL_PROFILE_FIELD_LABEL ffl
        on ffl.cl_customer_accounts_id=cu.cl_customer_accounts_id
    LEFT  JOIN CL_PROFILE_FIELD_VALUE ffv
        on ffl.id = ffv.profile_field_label_id  and ffv.user_id= cu.id
    LEFT  JOIN CL_PROFILE_FIELD_ASSIGNMENT ffa
        on ffl.id = ffa.profile_field_label_id
    where ffl.cl_customer_accounts_id = (
        select cl_customer_accounts_id
        from CL_USER where ID =  cu.id)
    and ffl.ENABLED = 'Y'
    and  u.PURCHASED_PRODUCT_ID  = 582002861
    and  REGEXP_LIKE  (u.topic,
        'difficult_interactions|customer_focus|leading_people' )
    group by cu.id, u.topic, cu.first_name, cu.last_name, cu.email,
    cu.REGISTRATION_DATE, trunc(u.view_date,'MONTH'), ffl.label,
    ffv.field_value
)
SELECT *
FROM   pivot_data
PIVOT (
     max(val)
     FOR label
     IN ('Flexfield1' AS Flexfield1, 'Flexfield2' AS Flexfield2,
         'Flexfield3' AS Flexfield3, 'Flexfield4' AS Flexfield4,
         'Flexfield5' AS Flexfield5, 'Flexfield6' AS Flexfield6,
         'flexfield021' AS flexfield021, 'sdcs' AS sdcs)
  )

我有动态数据创建的查询,即

SELECT DISTINCT 
    LISTAGG('''' || label || ''' AS ' || label,',')
        WITHIN GROUP (ORDER BY label) AS temp_in_statement 
    FROM (
        select distinct label
        from cl_profile_field_label
        where cl_customer_accounts_id=(
            select cl_customer_accounts_id
            from cl_purchased_product
            where id=582002861));

但是如果我将动态查询放在pivot IN子句中,我得到的错误为ORA-00936: missing expression。请帮帮我。

0 个答案:

没有答案