使用游标进行PL SQL打印

时间:2020-03-22 09:50:41

标签: sql oracle plsql cursor

我有一个表格,其中带有用于类别,日期和价格的列。像这样:

group 1  - 03.03.2019 - 5.00
group 1  - 03.02.2018 - 4.00
group 2  - 05.05.2019 - 2.25
group 2  - 05.05.2018 - 1.00

因此,每个组(几乎)总是有两个日期,价格不同。我有一个sql语句,该语句选择日期最接近给定日期的行,但是我不知道如何用光标将它们全部打印出来。因此06.06.2019的输出应如下所示:

group 1  - 03.03.2019 - 5.00
group 2  - 05.05.2019 - 2.25

因此它只打印一种类别+正确的价格(从正确的日期开始),但是打印10次。

1 个答案:

答案 0 :(得分:0)

当然可以,正如您所告知的那样。您要循环10次,并且-对于每次循环迭代-您打开/关闭相同的游标并打印其获取的值。

您应该做的是遍历游标本身;而且,正如您所说的,那应该是两个嵌套循环。像这样的东西(使它更清晰的伪代码):

DataFrame

应用于您的代码:

df1 = (df.set_index(np.zeros(len(df)))
         .reset_index()
         .set_axis(np.arange(len(df.columns) + 1), inplace=False, axis=1))
print (df1)
     0         1         2             3             4
0  0.0  0.498717  0.264786  9.923030e-03  5.168950e-04
1  0.0  0.427093  0.099070  1.071780e-03  2.753260e-05
2  0.0  0.276645  0.032204  1.123410e-04  1.604880e-06
3  0.0  0.148270  0.009288  1.097520e-05  9.280800e-08
4  0.0  0.097558  0.004401  2.865510e-06  1.838070e-08
5  0.0  0.030283  0.000649  1.040990e-07  3.586150e-10
6  0.0  0.021126  0.000372  4.072560e-08  1.191550e-10
7  0.0  0.008338  0.000092  4.052200e-09  8.087190e-12
8  0.0  0.028685  0.000597  9.021130e-08  3.030260e-10
9  0.0  0.000693  0.000003  1.431900e-11  1.226820e-14

out = df1.lookup(df1.index, v).tolist()
print (out)
[0.00992303, 0.0990702, 0.276645, 0.0, 1.83807e-08, 0.0, 0.0, 
 0.00833787, 0.0005966519999999999, 1.22682e-14]

尽管,请阅读您的实际要求,也许您想看看下面的代码,它比您的代码更简单:

样本数据:

begin
  for cur_1 as (select whatever that makes the first cursor) loop
    for cur_2 as (select whatever that makes the second cursor) loop
      dbms_output.put_line(value from cur_1 || value from cur_2);
    end loop;
  end loop;
end;

程序:

Procedure print_inf_value (closingDate Date) is
begin
  for cur_1 as (select t.attr
                from informationvalues t
                where t.dateofValue <= closingDate
                  and not exists (select 1
                                  from informationvalues t1
                                  where t1.attr = t.attr 
                                    and t1.dateofValue <= closingDate 
                                    and t1.dateofValue > t.dateofValue
               )
  loop
    for cur_2 as (select t.price
                  from informationvalues t
                  where t.dateofValue <= closingDate
                    and not exists (select 1
                                    from informationvalues t1
                                    where t1.attr = t.attr 
                                      and t1.dateofValue <= closingDate 
                                      and t1.dateofValue > t.dateofValue
                 )
    loop
      dbms_output.put_line('Attr: ' || cur_1.attr || '    Price: ' || cur_2.price);
    end loop;
  end loop;
end;

测试:

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL> select * From test order by grp, cdate;

       GRP CDATE           PRICE
---------- ---------- ----------
         1 03.02.2018          4
         1 03.03.2019          5
         2 05.05.2018          1
         2 05.05.2019       2,25