SQLPLUS中的LISTAGG函数对ORACLE 11g的错误

时间:2013-11-27 14:31:50

标签: oracle oracle11g sqlplus

我目前有这些表,我的主要目的是通过连接TABLE“ITEMS”中的category和item_number来自动填充event_item_exhibited列。 经过大量研究后,我尝试使用LISTAGG功能,但我在第2行遇到ERROR: ORA-00909:参数数量无效

CREATE TABLE ITEMOWNERS (itemOwner_number INT NOT NULL CHECK(itemOwner_number >= 1000 AND itemOwner_number <= 2999), 
          itemOwner_name VARCHAR(30) NOT NULL,                         
          itemOwner_address VARCHAR(100) NOT NULL,
          first_contact TIMESTAMP NOT NULL,                                                         
          PRIMARY KEY (itemOwner_number));



CREATE TABLE ITEMS (lent_by_itemOwner_number INT NOT NULL,
                    category CHAR (3) NOT NULL CONSTRAINT chk_category_input CHECK (category IN ('LDN','STH', 'MNC','SCT')),    
                    item_number NCHAR(3) NOT NULL UNIQUE,
                    item_name VARCHAR(30) NOT NULL UNIQUE,               
                    item_value NUMBER NOT NULL ,    
                    item_description VARCHAR(1000) NOT NULL,            
                    FOREIGN KEY (lent_by_itemOwner_number) REFERENCES ITEMOWNERS(itemOwner_number)); 

CREATE TABLE EVENTS (event_number INT NOT NULL CHECK(event_number >= 3000 AND event_number <= 3999),
                 event_item_exhibited VARCHAR(6) ,                                      
                 title  VARCHAR(100) NOT NULL UNIQUE,                            
                 event_description VARCHAR(1000) NOT NULL,                  
                 PRIMARY KEY (event_number));

我创建的功能如下:

SELECT event_number,
   LISTAGG(category, '/',item_number,',') WITHIN GROUP (ORDER BY category AND item_number) AS event_item_exhibited  
FROM (
   SELECT UNIQUE
          event_number,
     FROM EVENTS
   )
 GROUP BY event_number;

我附上了我的输出目标示例。 http://oi44.tinypic.com/2ur2dn4.jpg

以下是我在您推荐后所做的事情;

CREATE TABLE ITEMOWNERS (itemOwner_number INT NOT NULL CHECK(itemOwner_number >= 1000 AND itemOwner_number <= 2999), 
          itemOwner_name VARCHAR(30) NOT NULL,                         
          itemOwner_address VARCHAR(100) NOT NULL,
          first_contact TIMESTAMP NOT NULL,                                                         
          PRIMARY KEY (itemOwner_number));



CREATE TABLE ITEMS (lent_by_itemOwner_number INT NOT NULL,
                    category CHAR (3) NOT NULL CONSTRAINT chk_category_input CHECK (category IN ('LDN','STH', 'MNC','SCT')),    
                    item_number NCHAR(3) NOT NULL UNIQUE,
                    item_name VARCHAR(30) NOT NULL UNIQUE,               
                    item_value NUMBER NOT NULL ,    
                    item_description VARCHAR(1000) NOT NULL,            
                    FOREIGN KEY (lent_by_itemOwner_number) REFERENCES ITEMOWNERS(itemOwner_number)); 

CREATE TABLE EVENTS (event_number INT NOT NULL CHECK(event_number >= 3000 AND event_number <= 3999),
                 event_item_exhibited VARCHAR(7) ,                                      
                 title  VARCHAR(100) NOT NULL UNIQUE,                            
                 event_description VARCHAR(1000) NOT NULL,                  
                 PRIMARY KEY (event_number));


INSERT INTO ITEMOWNERS VALUES ('1001','Edward','London',CURRENT_TIMESTAMP);
INSERT INTO ITEMOWNERS VALUES ('1002','Michael','Nottingham',CURRENT_TIMESTAMP);



INSERT into ITEMS VALUES ('1001','LDN','121','Penmate','500','Unique pen');
INSERT into ITEMS VALUES ('1002','MNC','131','Trampoline','1500','Professional equipment');


INSERT INTO EVENTS VALUES ('3001','LDN/121','PEN SHOW', 'Exclusive show of unique pens');
INSERT INTO EVENTS VALUES ('3002','MNC/131','Jump Club', 'Enjoy a session of tampolining');

最终要求的输出就是这个。 http://oi39.tinypic.com/35a80on.jpg

ITEMOWNER_NUMBER  ITEMOWNER_NAME     ITEMOWNER_ADDRESS  FIRST_CONTACT                                            
        1001      Edward             London             27-NOV-13 15.14.43.899677                                                                          
        1002      Michael            Nottingham         27-NOV-13 15.14.43.930465                                                                           



LENT_BY_ITEMOWNER_NUMBER CATEGORY ITEM_NUMBER ITEM_NAME  ITEM_VALUE ITEM_DESCRIPTION

 1001                     LDN     121         Penmate    500        Unique pen
 1002                     MNC     131         Trampoline 1500       Professional equipment



   EVENT_NUMBER EVENT_ITEM  TITLE      EVENT_DESCRIPTION                                                                                               

           3001 LDN/121     PEN SHOW   Exclusive show of unique pens                                                                                           
           3002 MNC/131     Jump Club  Enjoy a session of tampolining                                                                                     

现在我的整个问题完全基于如何让数据库通过合并CATEGORY&amp;来自行填充EVENT_ITEM。 ITEM_NUMBER从TABLE ITEMS进入coloumn EVENT_ITEM,如上所示。

1 个答案:

答案 0 :(得分:2)

LISTAGG(category, '/',item_number,',') 
    WITHIN GROUP (ORDER BY category AND item_number) AS event_item_exhibited

那里的语法错误应该是

LISTAGG(category || '/' || item_number,',') 
    WITHIN GROUP (ORDER BY category , item_number) AS event_item_exhibited