UPDATE SQL查询中的聚合

时间:2016-01-18 15:07:53

标签: sql oracle join aggregation

所以,我目前有两个表,一个包含每个对象的固定部分,另一个包含每个对象的可变数量的属性作为键/值对。我想以JSON映射格式将这些属性转换为主表中的CLOB字段。

create table test_a (id integer, properties clob);
create table test_b (id integer, a_id integer, key char(30), value char(30));

insert into test_a values(1,'');
insert into test_a values(2,'');
insert into test_a values(3,'');

insert into test_b values (1, 1, 'k1', 'v1');
insert into test_b values (2, 1, 'k2', 'v2');
insert into test_b values (3, 2, 'k3', 'v3');
insert into test_b values (4, 2, 'k4', 'v4');
insert into test_b values (5, 2, 'k5', 'v5');
insert into test_b values (6, 2, 'k6', 'v6');

我可以通过以下查询构建我想要的JSON:

WITH PROPS AS
  (SELECT '"'
    ||trim(KEY)
    ||'":"'
    ||trim(value)
    ||'"' json,
    test_b.*
  FROM test_b
  )
SELECT test_a.id,
  '{'
  ||
  (SELECT listagg(json, ',') within GROUP (
  ORDER BY props.key)
  FROM PROPS
  WHERE PROPS.A_ID = test_a.id
  )
  || '}'
FROM TEST_A
INNER JOIN PROPS
ON TEST_A.ID = PROPS.A_ID
GROUP BY TEST_A.ID ;

我得到了理想的结果

1   "{""k1"":""v1"",""k2"":""v2""}"
2   "{""k3"":""v3"",""k4"":""v4"",""k5"":""v5"",""k6"":""v6""}"

但是当我尝试使用此结果插入主表时,我会收到错误

UPDATE TEST_A SET PROPERTIES = (
WITH PROPS AS
  (SELECT '"'
    ||trim(key)
    ||'":"'
    ||trim(value)
    ||'"' json,
    test_b.*
  FROM test_b
  )
SELECT '{'
  ||
  (SELECT listagg(json, ',') within GROUP (order by props.key) FROM PROPS
  WHERE PROPS.A_ID = test_a.id)
  || '}'  from TEST_A INNER JOIN PROPS ON TEST_A.ID = PROPS.A_ID GROUP BY TEST_A.ID
) ;

Error report -
SQL Error: ORA-01427: single-row subquery returns more than one row
01427. 00000 -  "single-row subquery returns more than one row"

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您需要相关的子查询而不是deque。整个外部查询看起来并不正确,所以我认为你需要这样的东西:

bool isPlayerIn(vector<Player*> players, int id) {
    vector<Player*>::iterator found = find_if(begin(players), end(players),
                                              [id] (Player* player) {
        return player->getId() == id;
        });
     return found != end(players);
}

bool isPlayerIn(map<int, Player*> players, int id) {
     map<int, Player*>::iterator found = find_if(begin(players), end(players),
                                                 [id] (pair<int, Player*> found) {
        return found.second->getId() == id;
        });
     return found != end(players);
}