选择位置(使用LISTAGG后)

时间:2017-05-04 07:04:44

标签: sql oracle

我想从oracle数据库中的不同表中运行此sql

表格A" SHIPMENT_LINE"

ORDERQTY_SHIP_ID    SHIP_LINE_ID
0                   SLM4298700
1                   SLM4286868
12                  SLM4162661
19                  SLM4162693

表格B =" pckwrk"

SHIP_LINE_ID    WRKREF
SLM4298700  
SLM4286868  W00003HAEM
SLM4162661  W00003DRGM
SLM4162693  W00003DRHO
SLM4162693  W00003DRHP

表格C =" invdtl"

WRKREF          SUBNUM
W00003DRHP  S00004979375
W00003DRHP  S00004979358
W00003DRHP  S00004979365
W00003HAEM  CTN2379829
W00003DRGM  S00004980093
W00003DRHO  S00004830248
W00003DRHP  S00004979369
W00003DRHP  S00004979346
W00003DRHP  S00004979333



   select a.ORDERQTY_SHIP_ID,a.SHIP_LINE_ID,a.wrkref,
                (CASE WHEN ORDERQTY_SHIP_ID>0  THEN 
                       (CASE WHEN INSTR(a.wrkref, ',', 1)=0 then  
                       (select LISTAGG(subnum, ''',''') WITHIN GROUP(ORDER BY wrkref) subnum 
                       from invdtl 
                       where wrkref in  ( a.wrkref )) --a.wrkref = "W00003DRGM"
                       else
                       (select LISTAGG(subnum, ''',''') WITHIN GROUP(ORDER BY wrkref)subnum 
                        from invdtl 
                       where wrkref in  (a.wrkref))                                
"a.wrkref  "W00003DRHO','W00003DRHP""
                      end)
                   else NULL
              end)SUBNUM
    from(select a.ORDERQTY_SHIP_ID,a.SHIP_LINE_ID,
           ( SELECT  LISTAGG(wrkref, ''',''') WITHIN GROUP(ORDER BY wrkref)wrkref
             FROM pckwrk
             WHERE SHIP_LINE_ID =a.SHIP_LINE_ID AND PRTNUM NOT LIKE'KITPART'
             GROUP BY SHIP_LINE_ID) as wrkref

    from (select a.ORDERQTY_SHIP_ID,a.SHIP_LINE_ID
          from SHIPMENT_LINE a)a)a

结果

ORDERQTY_SHIP_ID   SHIP_LINE_ID  WRKREF                   SUBNUM
0              SLM4298700    NULL                     NULL
1              SLM4286868    W00003HAEM           CTN2379829
12             SLM4162661    W00003DRGM           S00004980093
19             SLM4162693    W00003DRHO','W00003DRHP  NULL

在sql where a.wrkref=W00003DRHO','W00003DRHP (select LISTAGG(subnum, ''',''') WITHIN GROUP(ORDER BY wrkref)subnum from invdtl where wrkref in (a.wrkref))的这一部分是结果NULL而不是S00004979333','S00004979346','S00004979358','S00004979365','‌​S00004979369','S0000‌​4979375我如何在代价a.wrkref的开头和结尾添加单引号? p>

2 个答案:

答案 0 :(得分:0)

我的猜测是你试图使用in谓词将字符串匹配到子字符串,只有表达式相等才能成功。试试like,演示

select 1 from dual
where ''''||'W00003DRHO'',''W00003DRHP'||'''' like '%'''||'W00003DRHO'||'''%'

所以用{/ p>替换CASE WHEN INSTR(a.wrkref, ',', 1)=0 then ... end

(select LISTAGG(subnum, ''',''') WITHIN GROUP(ORDER BY wrkref) subnum 
 from invdtl 
 where  ''''+a.wrkref+'''' like '%'''||wrkref||'''%' )

答案 1 :(得分:0)

var dog = {
    name = 'Rex'
}

result = ok                        其他                        (选择LISTAGG(subnum,''',''')WITHIN GROUP(ORDER BY wrkref)subnum                         来自invdtl                        其中wrkref(a.wrkref))
“a.wrkref”W00003DRHO','W00003DRHP'结果= NULL(我想添加'for和after.wrkref之后'来获得'W00003DRHO','W00003DRHP'                       端)