2016-06-23 104 views
0

我在Jasperreport中遇到了SQL查詢問題。 查詢是這樣的:SQL查詢適用於開發人員,但不適用於JasperSoft Studio

select distinct 
    t.costcenter, 
    t.workplace, 
    t.sap_master_key, 
    t.product_family, 
    mb.max_module_name 
from 
    timings t, 
    max_bbz_per_timing mb 
where (
     ($P{Kostenstelle} is not null and 
     $P{APS} is not null 
     and t.timing_id in (
      select 
      timing_id 
      from 
      timings 
      where costcenter = $P{Kostenstelle} 
      and workplace = $P{APS} 
     ) 
     ) 
     or 
     ($P{Kostenstelle} is not null 
     and $P{APS} is null 
     and t.timing_id in (
      select 
      timing_id 
      from 
      timings 
      where costcenter = $P{Kostenstelle} 
     ) 
     ) 
     or 
     ($P{Kostenstelle} is null 
     and $P{APS} is not null 
     and t.timing_id in (
      select 
      timing_id 
      from 
      timings 
      where workplace = $P{APS} 
     ) 
     ) 
    ) 
and mb.timing_id =t.timing_id 
and mb.max_module_name is not null 

$P{Kostenstelle}$P{APS}是參數。他們都可以不是零或只有其中之一。當我在我的開發環境中嘗試這個SQL查詢時,它做它應該做的事情,但是在JasperSoft Studio中它只在$P{Kostenstelle} is not null and $P{APS} is null時執行,否則它不會顯示結果。

我希望有人能幫助我,我很無能。

+0

將是那些參數什麼樣的價值觀? – tobi6

+0

進一步來看,難道你不能只用一個'LEFT JOIN'和一個'ON'子句而不使用三塊檢查? – tobi6

+0

@ tobi6謝謝你的回答,但是Y.B的回答。爲我工作 – TheOneThing

回答

1

這個巨大的SQL腳本相當於只是:

select distinct 
    t.costcenter, 
    t.workplace, 
    t.sap_master_key, 
    t.product_family, 
    mb.max_module_name 
from timings t 
inner join max_bbz_per_timing mb 
    on mb.timing_id = t.timing_id and mb.max_module_name is not null 
inner join timings t_t 
    on t_t.timing_id = t.timing_id 
    and ($P{Kostenstelle} is not null or $P{APS} is not null) 
    and ($P{Kostenstelle} is null or t_t.costcenter = $P{Kostenstelle}) 
    and ($P{APS}   is null or t_t.workplace = $P{APS}) 

如果timing_id是唯一的查詢可以更加直接:

select distinct 
    t.costcenter, 
    t.workplace, 
    t.sap_master_key, 
    t.product_family, 
    mb.max_module_name 
from timings t 
inner join max_bbz_per_timing mb 
    on mb.timing_id = t.timing_id and mb.max_module_name is not null 
where ($P{Kostenstelle} is not null or $P{APS} is not null) 
    and ($P{Kostenstelle} is null or t.costcenter = $P{Kostenstelle}) 
    and ($P{APS}   is null or t.workplace = $P{APS}) 
+2

我試過了,現在可以使用了。我應該多閱讀一下'JOINS'。非常感謝 – TheOneThing

+0

沒有。我不確定這個't_t'過濾器。我這樣寫就是爲了複製原始查詢的確切行爲。如果(正如我懷疑的那樣)'timing_id'是唯一的,那麼查詢就更簡單了:只要用參數上的where子句過濾't'來代替上一個'inner join ... on ...'。 –

相關問題