Search code examples
postgresqljasper-reports

how to use aggregrate function in postgresql with join query and case when condition


I will put the below in jasper report using iReport and it works fine untill i will not left one of date value as a blank it will not produce result as accepted....

SELECT DISTINCT o.oid, o.serialnumber, o.product,o.quantity, o.price, o.orderdate, o.deliverydate,c.fname, c.lname
FROM ordert AS o INNER JOIN customer AS c ON
c.fname LIKE COALESCE(NULLIF(initcap($P{CustomerName}),''),'NONE') AND
o.id = c.id AND orderdate BETWEEN
CASE $P{fromdate}::DATE
WHEN NULL THEN
    (select min(orderdate) from ordert)
ELSE 
    $P{fromdate}::DATE
END
AND
CASE $P{todate}::DATE WHEN NULL
THEN
    (select max(orderdate) from ordert)
ELSE 
    $P{todate}::DATE
END
ORDER BY o.oid;

When Running above query and first date popup open so if i enter nothing in it so it must be choose minimum value from the database and if another date popup open and i will insert nothing in it so it must get max value from the table ....


please check the query whats wrong with it because it will work untill i left one of my date field blank


Solution

  • Unfortunately null do not equals null, null is undefined, nothing, you need to change your statement to something like this

    CASE ($P{fromdate}::DATE is null)
    WHEN true THEN
        (select min(orderdate) from ordert)
    ELSE 
        $P{fromdate}::DATE
    END