将列连接到行

时间:2010-08-25 20:04:15

标签: oracle database-design

这是我们购买的产品的情景。该产品允许我们创建自定义字段,但这些字段存储为CUSTOM表中的ROWS。

我想编写一个查询,该查询将连接到多个自定义字段并获取单行。

让我举个例子。

1) PERSON TABLE (ID int, NAME varchar2(30));
2) CUSTOMFIELDS TABLE(CFID int, CFTable varchar2(30), CFFieldName varchar2(30), CFFieldType varchar2(30));
3) CUSTOMFIELDVALUE TABLE(CFVID int, CFID int, CFFieldName varchar2(100), CFFieldValue varchar2(100));

我的人员表有一条记录

1) 1001 - Clark Kent

假设我为名为AGE和WEIGHT的人创建了两个自定义字段。 在这种情况下,将在CUSTOMFIELDS表中创建两条记录。

1) 100 - PERSON - AGE - INTEGER
2) 200 - PERSON - WEIGHT - INTEGER

现在这些自定义字段的值将像这样存储在CUSTOMFIELDVALUE表中。

1) 100 - 100 - 1001 - 44
2) 101 - 200 - 1001 - 200 lbs

我想写一个选择查询来获取像这样的记录

PERSON, AGE , WEIGHT
Clark Kent, 44, 200 lbs

我在想如何通过纯SQL实现这一目标。自定义字段的数量可以增加或减少,具体取决于产品的配置。

2 个答案:

答案 0 :(得分:0)

或许这样的事情:

select p.name, age.CFFieldValue, weight.CFFieldValue
from person p
inner join CUSTOMFIELDVALUE age
 on p.id = age.CFFieldName
inner join CUSTOMFIELDS age_f
 on age_f.cfid = age.cfid and age_f.CFFieldName = 'AGE' and age_f.CFTable = 'PERSON'
inner join CUSTOMFIELDVALUE weight
 on p.id = weight.CFFieldName and weight_f.CFFieldName = 'AGE' and weight_f.CFTable = 'PERSON'
inner join CUSTOMFIELDS weight_f
 on weight_f.cfid = weight.cfid;

我不完全清楚person.id是否加入customfieldvalue.cfieldname,但你确实说100 - 100 - 1001 - 44所以我猜你试图说1001是第三列吗?本质上,查询过滤customfieldvalues表并将其用作两个单独的表。我认为这就是你想要的 - 你的问题并不是很清楚。

答案 1 :(得分:0)

这是一个有趣的问题。您想要动态更改列的数量和名称。使用“普通”SQL无法创建。我尝试使用PIPELINED FUNCTION创建样本。

我首先创建了表:

CREATE TABLE PERSON (ID int, NAME varchar2(30));
CREATE TABLE CUSTOMFIELDS (CFID int, CFTable varchar2(30), CFFieldName varchar2(30), CFFieldType varchar2(30));
CREATE TABLE CUSTOMFIELDVALUE (CFVID int, CFID int, CFFieldName varchar2(100), CFFieldValue varchar2(100));

INSERT INTO PERSON(id, name) values(1001, 'Clark Kent');
INSERT INTO CUSTOMFIELDS(CFID, CFTable, CFFieldName, CFFieldType) values(100, 'PERSON', 'AGE', 'INTEGER');
INSERT INTO CUSTOMFIELDS(CFID, CFTable, CFFieldName, CFFieldType) values(200, 'PERSON', 'WEIGHT', 'INTEGER');

......我放了一些数据:

INSERT INTO CUSTOMFIELDVALUE (CFVID, CFID, CFFieldName, CFFieldValue) values(100, 100, 1001, 44);
INSERT INTO CUSTOMFIELDVALUE (CFVID, CFID, CFFieldName, CFFieldValue) values(101, 200, 1001, 200);

然后我创建了一个对象类型:

CREATE TYPE CustomFieldType AS OBJECT
(
  row_id number,
  fieldType varchar2(200),
  person_id number,
  fieldValue1 varchar2(2000),
  fieldValue2 varchar2(2000),
  fieldValue3 varchar2(2000),
  fieldValue4 varchar2(2000),
  fieldValue5 varchar2(2000)
)
/

CREATE TYPE CustomFieldTypeSet AS TABLE OF CustomFieldType
/

还创建了流水线功能:

CREATE OR REPLACE
    FUNCTION GET_PERSON_FIELDS(person_id_in   IN NUMBER
                                    ,field_names_in IN VARCHAR2) RETURN CustomFieldTypeSet
        PIPELINED
    IS

        -- constructor CustomFieldType()
        l_header_row          CustomFieldType := CustomFieldType(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
        l_data_row            CustomFieldType := CustomFieldType(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
        l_tablen              BINARY_INTEGER;
        l_tab                 DBMS_UTILITY.uncl_array;
        l_num_of_field_values PLS_INTEGER := 5;
        l_counter             PLS_INTEGER := 1;
        l_position            PLS_INTEGER;
        l_field_names_in      VARCHAR2(2000) := field_names_in;

        TYPE type_header_hash IS TABLE OF PLS_INTEGER INDEX BY VARCHAR2(200);
        l_header_hash type_header_hash;

    BEGIN

        -- 1) check, what fields you can display

        IF (l_field_names_in IS NULL) THEN

            <<get_all_fields>>
            FOR cur_all_fields IN  (SELECT DISTINCT flds.CFFIELDNAME
                                               FROM CUSTOMFIELDS flds
                                                   ,CUSTOMFIELDVALUE cfv
                                              WHERE cfv.CFID = flds.CFID
                                                AND flds.CFTable = 'PERSON' ) LOOP
                l_field_names_in := l_field_names_in ||
                                    cur_all_fields.CFFIELDNAME ||
                                    ',';
            END LOOP get_all_fields;

        END IF;

        -- 2) generate header (function RTRIM prevent ORA-00931 exception!)

        DBMS_UTILITY.comma_to_table(list => RTRIM(l_field_names_in, ','), tablen => l_tablen, tab => l_tab);
        l_header_row.row_id := 1;
        l_header_row.fieldType := 'HEADER';

        <<header_cursor>>
        FOR i IN 1..l_tablen LOOP

            IF (i = 1) THEN
                l_header_row.fieldValue1 := l_tab(i);
                l_header_hash(l_tab(i)) := i;
            ELSIF (i = 2) THEN
                l_header_row.fieldValue2 := l_tab(i);
                l_header_hash(l_tab(i)) := i;
            ELSIF (i = 3) THEN
                l_header_row.fieldValue3 := l_tab(i);
                l_header_hash(l_tab(i)) := i;
            ELSIF (i = 4) THEN
                l_header_row.fieldValue4 := l_tab(i);
                l_header_hash(l_tab(i)) := i;
            ELSIF (i = 5) THEN
                l_header_row.fieldValue5 := l_tab(i);
                l_header_hash(l_tab(i)) := i;
            END IF;

        END LOOP header_cursor;

        -- 3) print data to SQL (over pipe)...

        PIPE ROW(l_header_row);

        FOR cur_persons IN  (SELECT ID
                               FROM PERSON
                              WHERE ID = COALESCE(person_id_in, ID) ) LOOP
            l_data_row.row_id := NULL;
            l_data_row.person_id := NULL;
            l_data_row.fieldType := NULL;
            l_data_row.fieldValue1 := NULL;
            l_data_row.fieldValue2 := NULL;
            l_data_row.fieldValue3 := NULL;
            l_data_row.fieldValue4 := NULL;
            l_data_row.fieldValue5 := NULL;
            l_data_row.fieldType := 'DATA';
            FOR cur_data IN  (SELECT p.ID AS person_id
                                    ,cfv.CFID
                                    ,flds.CFTABLE
                                    ,flds.CFFIELDNAME
                                    ,cfv.CFFIELDVALUE
                                FROM PERSON p
                                    ,CUSTOMFIELDS flds
                                    ,CUSTOMFIELDVALUE cfv
                               WHERE p.ID = cur_persons.ID
                                 AND p.ID = cfv.CFFIELDNAME
                                 AND cfv.CFID = flds.CFID ) LOOP
                l_data_row.person_id := cur_persons.ID;
                l_position := NULL;

                IF (l_header_hash.EXISTS(cur_data.CFFIELDNAME)) THEN
                    l_position := l_header_hash(cur_data.CFFIELDNAME);
                END IF;

                IF (l_position = 1) THEN
                    l_data_row.fieldValue1 := cur_data.CFFIELDVALUE;
                ELSIF (l_position = 2) THEN
                    l_data_row.fieldValue2 := cur_data.CFFIELDVALUE;
                ELSIF (l_position = 3) THEN
                    l_data_row.fieldValue3 := cur_data.CFFIELDVALUE;
                ELSIF (l_position = 4) THEN
                    l_data_row.fieldValue4 := cur_data.CFFIELDVALUE;
                ELSIF (l_position = 5) THEN
                    l_data_row.fieldValue5 := cur_data.CFFIELDVALUE;
                END IF;

            END LOOP;
            l_counter := l_counter + 1;
            l_data_row.row_id := l_counter;
            PIPE ROW(l_data_row);
        END LOOP;

        RETURN;
    END GET_PERSON_FIELDS;

您可以使用SQL获取示例数据(注意:防止异常ORA-22905,您必须设置会话变量“ALTER SESSION SET CURSOR_SHARING = EXACT;”):

SELECT * FROM TABLE(GET_PERSON_FIELDS(1001,'AGE,WEIGHT'));

这是输出:

ROW_ID FIELDTYPE  PERSON_ID FIELDVALUE FIELDVALUE FIELDVALUE
------ ---------- --------- ---------- ---------- ----------
     1 HEADER               AGE
     2 DATA            1001 44

在第一列是标题,其中存储有关字段名称的信息,并且在标题之后存储数据。您可以使用这些SQL的组合:

SELECT * FROM TABLE(GET_PERSON_FIELDS(1001,'AGE,WEIGHT'));
SELECT * FROM TABLE(GET_PERSON_FIELDS(1002,'AGE,GENDER'));
SELECT * FROM TABLE(GET_PERSON_FIELDS(1001,NULL));
SELECT * FROM TABLE(GET_PERSON_FIELDS(NULL,NULL));
  1. 第一个参数是person_id
  2. 第二个参数是您要查看输出的项目列表
  3. 如果第一个参数为NULL,则可以看到所有人的列表
  4. 如果第二个参数为NULL,则可以看到所有参数的列表
  5. 脚本不完整,有一些限制:
      对象CustomFieldType中的
    1. 你不能动态改变字段数(我的意思是fieldValue1,fieldValue2 ......)
    2. 在函数体GET_PERSON_FIELDS中,如你所见,动态问题也在IF语句中(IF(l_position = 1)THEN l_data_row.fieldValue1,IF(l_position = 2)THEN l_data_row.fieldValue1)...
  6. 最后,当我输入一些样本数据时,如下:

    INSERT INTO PERSON(id, name) values(1002, 'Lois Lane');
    INSERT INTO CUSTOMFIELDS(CFID, CFTable, CFFieldName, CFFieldType) values(300, 'PERSON', 'GENDER', 'VARCHAR');
    INSERT INTO CUSTOMFIELDS(CFID, CFTable, CFFieldName, CFFieldType) values(400, 'PERSON', 'SINGLE', 'VARCHAR');
    INSERT INTO CUSTOMFIELDVALUE (CFVID, CFID, CFFieldName, CFFieldValue) values(102, 100, 1002, 45);
    INSERT INTO CUSTOMFIELDVALUE (CFVID, CFID, CFFieldName, CFFieldValue) values(103, 300, 1002, 'FEMALE');
    INSERT INTO CUSTOMFIELDVALUE (CFVID, CFID, CFFieldName, CFFieldValue) values(104, 400, 1002, 'YES');
    

    ...并运行此SQL命令:

    SELECT * FROM TABLE(GET_PERSON_FIELDS(NULL,NULL));
    

    ...输出看起来像这样:

    ROW_ID FIELDTYPE  PERSON_ID FIELDVALUE FIELDVALUE FIELDVALUE FIELDVALUE
    ------ ---------- --------- ---------- ---------- ---------- ----------
         1 HEADER               AGE        GENDER     SINGLE     WEIGHT
         2 DATA            1001 44                               200
         3 DATA            1002 45         FEMALE     YES