这是我正在尝试做的事情和获得的最简单的工作示例:
我有一个查询,如下所示:
/*
with tran_party as -- ALL DUMMY DATA ARE IN THESE CTE FOR YOUR REFERENCE
(select 1 tran_party_id, 11 transaction_id, 101 team_id_redirect
from dual
union all
select 2, 11, 101 from dual
union all
select 3, 11, 102 from dual
union all
select 4, 12, 103 from dual
union all
select 5, 12, 103 from dual
union all
select 6, 12, 104 from dual
union all
select 7, 13, 104 from dual
union all
select 8, 13, 105 from dual),
tran as
(select 11 transaction_id, 1001 account_id, 1034.93 amount from dual
union all
select 12, 1001, 2321.89 from dual
union all
select 13, 1002, 3201.47 from dual),
account as
(select 1001 account_id, 111 team_id from dual
union all
select 1002, 112 from dual),
team as
(select 101 team_id, 'UUU' as team_code from dual
union all
select 102, 'VV' from dual
union all
select 103, 'WWW' from dual
union all
select 104, 'XXXXX' from dual
union all
select 105, 'Z' from dual)
-- */
-- The Actual Query
select a.account_id,
t.transaction_id,
(select listagg (tm_redir.team_code, ', ')
within group (order by tm_redir.team_code)
from tran_party tp_redir
inner join team tm_redir
on tp_redir.team_id_redirect = tm_redir.team_id
inner join tran t_redir
on tp_redir.transaction_id = t_redir.transaction_id
where t_redir.account_id = a.account_id
and t_redir.transaction_id != t.transaction_id)
as teams_redirected
from tran t inner join account a on t.account_id = a.account_id;
注意: tran_party.team_id_redirect 是引用 team.team_id 的外键。
电流输出:
ACCOUNT_ID TRANSACTION_ID TEAMS_REDIRECTED
---------- -------------- ----------------
1001 11 WWW, WWW, XXXXX
1001 12 UUU, UUU, VV
1002 13
预期产量:
我希望TEAMS_REDIRECTED
列中的重复项仅被选择一次,如下所示:
ACCOUNT_ID TRANSACTION_ID TEAMS_REDIRECTED
---------- -------------- ----------------
1001 11 WWW, XXXXX
1001 12 UUU, VV
1002 13
我试过的
tran_party
我写了一个内联视图,而不是直接从中选择,而是从tran_party中选择不同的值,如下所示:
select a.account_id,
t.transaction_id,
(select listagg (tm_redir.team_code, ', ')
within group (order by tm_redir.team_code)
from (select distinct transaction_id, team_id_redirect -- Note this inline view
from tran_party) tp_redir
inner join team tm_redir
on tp_redir.team_id_redirect = tm_redir.team_id
inner join tran t_redir
on tp_redir.transaction_id = t_redir.transaction_id
where t_redir.account_id = a.account_id
and t_redir.transaction_id != t.transaction_id)
as teams_redirected
from tran t inner join account a on t.account_id = a.account_id;
尽管这确实给了我预期的输出,但是当我在实际代码中使用此解决方案时,只需要一行大约13秒钟即可检索到一行。因此,我无法使用已经尝试过的东西。
任何帮助将不胜感激。
以下方法摆脱了嵌入式视图以获取重复项,它使用REGEXP_REPLACE
和RTRIM
在LISTAGG
函数上获取中的不同结果集aggregated list
。因此,它不会进行多次扫描。
将此片段添加到您的代码中,
RTRIM(REGEXP_REPLACE(listagg (tm_redir.team_code, ',')
WITHIN GROUP (ORDER BY tm_redir.team_code),
'([^,]+)(,\1)+', '\1'),
',')
修改后的查询-
SQL> with tran_party as -- ALL DUMMY DATA ARE IN THESE CTE FOR YOUR REFERENCE
2 (select 1 tran_party_id, 11 transaction_id, 101 team_id_redirect
3 from dual
4 union all
5 select 2, 11, 101 from dual
6 union all
7 select 3, 11, 102 from dual
8 union all
9 select 4, 12, 103 from dual
10 union all
11 select 5, 12, 103 from dual
12 union all
13 select 6, 12, 104 from dual
14 union all
15 select 7, 13, 104 from dual
16 union all
17 select 8, 13, 105 from dual),
18 tran as
19 (select 11 transaction_id, 1001 account_id, 1034.93 amount from dual
20 union all
21 select 12, 1001, 2321.89 from dual
22 union all
23 select 13, 1002, 3201.47 from dual),
24 account as
25 (select 1001 account_id, 111 team_id from dual
26 union all
27 select 1002, 112 from dual),
28 team as
29 (select 101 team_id, 'UUU' as team_code from dual
30 union all
31 select 102, 'VV' from dual
32 union all
33 select 103, 'WWW' from dual
34 union all
35 select 104, 'XXXXX' from dual
36 union all
37 select 105, 'Z' from dual)
38 -- The Actual Query
39 select a.account_id,
40 t.transaction_id,
41 (SELECT RTRIM(
42 REGEXP_REPLACE(listagg (tm_redir.team_code, ',')
43 WITHIN GROUP (ORDER BY tm_redir.team_code),
44 '([^,]+)(,\1)+', '\1'),
45 ',')
46 from tran_party tp_redir
47 inner join team tm_redir
48 on tp_redir.team_id_redirect = tm_redir.team_id
49 inner join tran t_redir
50 on tp_redir.transaction_id = t_redir.transaction_id
51 where t_redir.account_id = a.account_id
52 and t_redir.transaction_id != t.transaction_id)
53 AS teams_redirected
54 from tran t inner join account a on t.account_id = a.account_id
55 /
ACCOUNT_ID TRANSACTION_ID TEAMS_REDIRECTED
---------- -------------- --------------------
1001 11 WWW,XXXXX
1001 12 UUU,VV
1002 13
SQL>
我将以下SQL作为本机查询运行,但我想知道是否有一种方法可以在JPAQuery中运行它,以便使用元组或类实例化。 为了精确起见,我使用了别名而不是qtypes。
问题内容: 将SELECT查询放在UPDATE查询的WHERE子句中时出现错误。 我的查询是这样的: AND错误将显示如下: “无法在FROM子句中指定目标表’m’进行更新” 问题答案: 实际上,您可以通过将其包装在子查询中来更新它(从而为结果创建临时表) 或使用 JOIN
我有稍微不同的选择查询从同一个表中读取数据。 和正在更改。 在Laravel中,我最终为每种类型使用了1个查询,但是有10种类型,这意味着10 db连接——这不好。 将这些查询合并到一个查询中,以便建立一个数据库连接以获取所有数据的好方法是什么? (我正在寻找Laravel查询生成器实现这一点的方法)
我试图从表文章列表的文章与类别列表,每篇文章是列出的。假设文章“A”有3个类别“1”,“2”,“3”,我有一个以上的文章有一个以上的类别。我想在ArticleViewModel的列表中获得CategoryViewModel的列表。因为它是IEnumerableList,所以当我使用FirstOrDefault()时,我会在每个文章下获得相同的类别名称,但获得的文章数量是正确的。我如何才能获得类别名
我是sql的新手,我需要帮助这个例子使用子查询。 我需要在Amanda和Uamanda下获取所有customerId,但现在我只有这个CustomerID100456。 我如何使用这个customerid100456 combain子查询来获取Amanda和uamanda下的所有数据。 这是我想要的预期结果
我当前的代码: 但是我要一个java。lang.IllegalArgumentException。 我需要的是在用户表中显示一列。 类似于但是帐户实体没有只有User实体才有的getName()。 *更新 这就是错误所在 Java语言lang.IllegalArgumentException:在EntityManager中创建查询时发生异常:异常描述:语法错误分析查询[SELECT a Accou