在一个 Table 到另一个 Table 中使用循环
Using Loops in one Table through other Table
有两个 TableA 和 B。
Table答:
TableB
现在,常规和凌乱的 SQL 获得所需的输出如下所示:
select person, discount from a where Age >18 and category = 'Gold'
union all
select person, discount from a Age = 18 and category = 'General'
Table A 包括多个其他 characteristics/measures 人并且是 100 万行。
Table B 包含多个折扣计算规则。
目前,我正在通过手动使用 union all 语句来解决这个问题。
请告知是否有任何 PL/SQL 或存储过程可能正在使用 Amazon Redshift 来解决相同问题。
在我看来,无论您做什么 - 您都需要动态 SQL,否则您将无法使用这些过滤器。一个选项可能是 returns 引用光标的函数。
当您对要返回的列进行硬编码时(顺便说一句,您发布的查询无效;tablea
中没有 discount
列 - 它属于 tableb
,因此您将需要加入),我也这样做了。请注意 - 因为它是动态的 SQL - 你可以存储 select
列列表,from
子句,就像你存储过滤器(where
子句,基本上)并且有一切动态。
好的,我们开始吧。首先是示例表:
SQL> select * from tablea;
PERSON AGE CATEGORY
---------- ---------- ----------
A 18 General
B 20 Genera
C 30 Silver
D 40 Silver
E 50 Bronze
F 60 Gold
6 rows selected.
SQL> select * from tableb;
FILTER DISCOUNT
-------------------------- ----------
Age>18 and category='Gold' 500
Age=18 and category='Gold' 1000
SQL>
函数:
SQL> create or replace function f_amazon
2 return sys_refcursor
3 is
4 l_rc sys_refcursor;
5 l_str varchar2(32000);
6 begin
7 for cur_r in (select b.filter from tableb b) loop
8 l_str := l_str ||
9 'select a.person, a.age, a.category, b.discount ' ||
10 ' from tablea a join tableb b on ' || cur_r.filter ||
11 ' union all ';
12 end loop;
13
14 l_str := rtrim(l_str, ' union all ');
15
16 open l_rc for l_str;
17 return l_rc;
18 end;
19 /
Function created.
测试:
SQL> select f_amazon from dual;
F_AMAZON
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
PERSON AGE CATEGORY DISCOUNT
---------- ---------- ---------- ----------
F 60 Gold 500
F 60 Gold 1000
SQL>
结果在这里,所以我想它有点 有效。
有两个 TableA 和 B。
Table答:
TableB
现在,常规和凌乱的 SQL 获得所需的输出如下所示:
select person, discount from a where Age >18 and category = 'Gold'
union all
select person, discount from a Age = 18 and category = 'General'
Table A 包括多个其他 characteristics/measures 人并且是 100 万行。
Table B 包含多个折扣计算规则。
目前,我正在通过手动使用 union all 语句来解决这个问题。
请告知是否有任何 PL/SQL 或存储过程可能正在使用 Amazon Redshift 来解决相同问题。
在我看来,无论您做什么 - 您都需要动态 SQL,否则您将无法使用这些过滤器。一个选项可能是 returns 引用光标的函数。
当您对要返回的列进行硬编码时(顺便说一句,您发布的查询无效;tablea
中没有 discount
列 - 它属于 tableb
,因此您将需要加入),我也这样做了。请注意 - 因为它是动态的 SQL - 你可以存储 select
列列表,from
子句,就像你存储过滤器(where
子句,基本上)并且有一切动态。
好的,我们开始吧。首先是示例表:
SQL> select * from tablea;
PERSON AGE CATEGORY
---------- ---------- ----------
A 18 General
B 20 Genera
C 30 Silver
D 40 Silver
E 50 Bronze
F 60 Gold
6 rows selected.
SQL> select * from tableb;
FILTER DISCOUNT
-------------------------- ----------
Age>18 and category='Gold' 500
Age=18 and category='Gold' 1000
SQL>
函数:
SQL> create or replace function f_amazon
2 return sys_refcursor
3 is
4 l_rc sys_refcursor;
5 l_str varchar2(32000);
6 begin
7 for cur_r in (select b.filter from tableb b) loop
8 l_str := l_str ||
9 'select a.person, a.age, a.category, b.discount ' ||
10 ' from tablea a join tableb b on ' || cur_r.filter ||
11 ' union all ';
12 end loop;
13
14 l_str := rtrim(l_str, ' union all ');
15
16 open l_rc for l_str;
17 return l_rc;
18 end;
19 /
Function created.
测试:
SQL> select f_amazon from dual;
F_AMAZON
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
PERSON AGE CATEGORY DISCOUNT
---------- ---------- ---------- ----------
F 60 Gold 500
F 60 Gold 1000
SQL>
结果在这里,所以我想它有点 有效。