1

我在 Oracle 11g 中有 3 个字段的表:

 STUDYID  |  STUDY_PARAMETER  | STUDY_VALUE

  5268    |  Age Group        | ADULT (18-65)
  5269    |  Age Group        | ADULT (18-65)
  5270    |  Age Group        | ADULT (18-65)
  5271    |  Age Unit         | ADULT (18-65)    
  1668A   |  Trial Type       | ADULT (18-65)
  5273    |  Trial Type       | Dispensing
  5345    |  Age Unit         | Years
  1668AC  |  Age Group        | ADULTS (18-39)

所以,我需要按以下顺序显示值:

STUDY_ID  |  AGE_GROUP     |  AGE_UNIT |  TRIAL_TYPE

  5268    |  ADULT (18-65) |  Years    | Dispensing
  5269    |  ADULT (18-65) |  (null)   | (null)
  1668AC  |  ADULTS (18-39)|  Years    | Non - Dispensing

等等。

到目前为止,我所拥有的是:

SELECT *
FROM  (
        SELECT STUDYID, STUDY_VALUE, STUDY_PARAMETER
        FROM   RD.STUDY_INFO
      )
PIVOT (
        SUM(STUDY_VALUE)
        FOR (STUDY_PARAMETER) 
        IN (
              'Age Unit' AS AGE_UNIT,
              'Age Group' AS AGE_GROUP,
              'Trial Type' AS TRIAL_TYPE
           )
      );

我从网上的例子中学到了这一点,但我不确定我是否可以像这样使用 SUM() ......?!

我收到此错误:

ORA-01722: invalid number
01722. 00000 -  "invalid number"

有谁看到我做错了什么?

4

2 回答 2

3

由于该STUDY_VALUE列似乎是一个字符串,因此您需要对这些值使用max()min()聚合函数:

SELECT *
FROM  
(
  SELECT STUDYID, STUDY_VALUE, STUDY_PARAMETER
  FROM   STUDY_INFO
)
PIVOT 
(
  MAX(STUDY_VALUE)
  FOR (STUDY_PARAMETER) IN ('Age Unit' AS AGE_UNIT,
                              'Age Group' AS AGE_GROUP,
                              'Trial Type' AS TRIAL_TYPE)
);

请参阅带有演示的 SQL Fiddle

于 2013-06-15T23:24:30.183 回答
2

你可以试试这个查询。

SELECT 
ID,
MAX(Case When parameter='Age Group' then Value else '0' end) AS  AgeGroup,
MAX(Case When parameter='Trial Type' then Value else '0'end)AS  TrialType,
MAX(Case When parameter='Age Unit' then Value else '0'end)AS  AgeUnit
FROM teststack
GROUP BY ID 
ORDER BY ID
于 2013-06-17T06:42:25.607 回答