将参数传递给oracle plsql函数

时间:2012-01-31 11:19:44

标签: oracle function oracle10g oracle-apex

我写过两个问题。

查询1:

select stock_id from_id, 
       nvl(lead(stock_id, 1, null) over (order by stock_date),stock_id) to_id
  from STOCK
 where stock_time not like 'NEW_STOCK';

上述查询返回如下结果:

FROM_ID TO_ID
331     341
341     351
351     361
361     371
371     391
391     401
401     421
421     441
441     451
451     461
461     321
321     323
323     491
491     501
501     501

我的第二个问题是

 select max(m.material_thikness) || 
        ' mm' || ' | ' || 
        m.material_guage || 
        ' g' guage,    
        sum(util.find_usage_from_stock(MATERIAL_THIKNESS,P_FROM_ID,P_TO_ID)) -
        sum(util.find_sheets_sold(MATERIAL_THIKNESS,P_FROM_ID,P_TO_ID)) diff
  from material m
 where m.active like 'Y'
 group by m.material_guage
 order by m.material_guage;

以上查询返回如下结果:

GUAGE           DIFF
6 mm | 11 g     0
4 mm | 12 g     -0.32
3 mm | 13 g     0.51
2 mm | 14 g     0.85
1.5 mm | 16 g   -0.41
1.2 mm | 18 g   0.35
1 mm | 19 g     1.67
.8 mm | 20 g    0

我需要在第二次查询中将FROM_IDTO_ID从我的第一个查询添加到P_FROM_IDP_TO_ID,我需要修改第二个查询才能获得结果如下。

RANGE     DIFF
331-341   0
341-351   0.35
351-361   0.45
.......
441-451   1.25
451-461   -0.75
461-321   1.67

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

如果要为所有p_id间隔获取每个material_thikness的值,则:

select  s.from_id || '-' ||s.to_id range,
        sum(util.find_usage_from_stock(MATERIAL_THIKNESS,s.from_id,s.to_id)) -
        sum(util.find_sheets_sold(MATERIAL_THIKNESS,s.from_id,s.to_id)) diff
  from material m, (select stock_id from_id, 
                                         lead(stock_id, 1, stock_id) 
                                             over (order by stock_date) to_id
                                    from STOCK
                                   where stock_time not like 'NEW_STOCK') s
 where m.active like 'Y'
 group by s.from_id,s.to_id
 order by s.from_id;

答案 1 :(得分:0)

你在寻找这样的东西:

 select max(m.material_thikness) || 
        ' mm' || ' | ' || 
        m.material_guage || 
        ' g' guage,    
        sum(util.find_usage_from_stock(MATERIAL_THIKNESS,P_FROM_ID,P_TO_ID)) -
        sum(util.find_sheets_sold(MATERIAL_THIKNESS,P_FROM_ID,P_TO_ID)) diff
  from material m
 where m.active like 'Y'
   and (m.p_from_id,m.p_to_id) in(select stock_id from_id, 
                                         nvl(lead(stock_id, 1, null) 
                                             over (order by stock_date),stock_id) to_id
                                    from STOCK
                                   where stock_time not like 'NEW_STOCK')
 group by m.material_guage
 order by m.material_guage;

会这样做吗?