【问题标题】:PL SQL Query NVL with Multiple values separated by commasPL SQL查询NVL,多个值用逗号分隔
【发布时间】:2017-03-10 02:48:18
【问题描述】:

我有一个查询允许用户按日期范围、商店(一个商店编号)和邮政编码进行选择,

关于商店,我希望能够输入多个用逗号分隔的商店编号。

以下代码适用于单个商店,但不适用于多个商店编号

      SELECT tt.id_str_rt store
           ,SUBSTR(tt.inf_ct,1,5) zip_code
           ,COUNT(tt.ai_trn) tran_count
           ,SUM(tr.mo_nt_tot) sales_value
          FROM orco_owner.tr_trn tt
           ,orco_owner.tr_rtl tr
         WHERE tt.id_str_rt = tr.id_str_rt


          AND (tt.id_str_rt IN NVL(:PM_store_number,tt.id_str_rt) OR :PM_store_number IS NULL)


           AND NVL(SUBSTR(tt.inf_ct,1,5),0) = NVL(:PM_zip_code,NVL(SUBSTR(tt.inf_ct,1,5),0))
           AND tt.id_ws = tr.id_ws
           AND tt.dc_dy_bsn = tr.dc_dy_bsn
           AND tt.ai_trn = tr.ai_trn
                  AND TRUNC(TO_DATE(tt.dc_dy_bsn,'yyyy-MM-dd'))
          BETWEEN NVL(:PM_date_from, TRUNC(TO_DATE(tt.dc_dy_bsn,'yyyy-MM-dd')))
              AND NVL(:PM_date_to,TRUNC(TO_DATE(tt.dc_dy_bsn,'yyyy-MM-dd')))
           AND LENGTH(TRIM(TRANSLATE(SUBSTR(inf_ct,1,5), '0123456789', ' '))) IS NULL
          GROUP BY tt.id_str_rt,SUBSTR(tt.inf_ct,1,5)
          ORDER BY zip_code, store

【问题讨论】:

  • 您在寻找coalesce(a, b, c, d, . . . .)吗?
  • 更多 - 你指的是这部分吗? NVL(:PM_store_number,tt.id_str_rt) 即多个店铺号作为参数?

标签: sql oracle


【解决方案1】:

您可以创建一个类似how to convert csv to table in oracle 中描述的函数:

create or replace function splitter(p_str in varchar2) return  sys.odcivarchar2list
is
v_tab sys.odcivarchar2list:=new sys.odcivarchar2list();
begin
with cte as (select level  ind from dual
connect by 
level <=regexp_count(p_str,',') +1
)
select regexp_substr(p_str,'[^,]+',1,ind)
bulk collect into v_tab
from cte;
return v_tab;
end;
/

然后你会像这样在你的查询中使用它:

and (tt.id_str_rt in (select column_value from table(splitter(:PM_store_number)) ))

而不是这个:

AND (tt.id_str_rt IN NVL(:PM_store_number,tt.id_str_rt) OR :PM_store_number IS NULL)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2010-10-05
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多