简体   繁体   中英

Single parameter multiple records select statement sql

I'm trying to consolidate reports. I have 2 identical reports using similar sql statements. The only difference is the way the input parameter is used.

In the first report the user inputs the parent number (111) and it selects all child orders between 111.000 and 111.999. If a user inputs the parent and child (ie 111.013), all children are returned from 111.013 to 111.999.

Statement 1:

SELECT ordernumber
FROM table1
WHERE  ordernumber >= $P{Parameter1} 
and ordernumber < $P{Parameter1} +'1'

The second statement selects only a single order given the parent and child (111.013).

Statement 2:

  SELECT ordernumber
    FROM table1
    WHERE  ordernumber = $P{Parameter1} 

Is there a way to combine these statements to use the different inputs to get the correct output?

If you pass in one more parameter, lets say $P{Parameter2}, which indicates which statement to be used. As in if $P{Parameter2} is true, use statement 1 else use statement 2. The following query can be used.

SELECT ordernumber FROM table1 WHERE (ordernumber >= $P{Parameter1} and ordernumber < $P{Parameter1} +1 and $P{Parameter2}) or (ordernumber = $P{Parameter1} and !$P{Parameter2})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM