I don't know how to title this question and I know it's just a simple and stupid logic need to be sorted out but I can explain what I need. I have a jasper report script and query in which I need a simple calculation based on a value gathered from Query below
SELECT TOTAL, PARTIAL FROM PRICE WHERE TOTAL > 0
now I need this value to be calculated as the expression below in jasper report script
VAR CALC += TOTAL + (PARTIAL) //as partial can be a -ve or +ve value
What's happening currently is that I didn't find a way to do this, whenever I assign value of TOTAL to a variable and try to use it, it always get's the value from Query and calculates, while I need it to be there only once and then perform calculation on it onward. For that I tried to use calculation="First" but that also gives the first value for all the time and continues. I hope I am able to put my problem well, please help
EDIT
QUERY
SELECT RECEIPTS.DATENEW AS DATE,
TICKETS.TICKETID AS TICKETID,
PAYMENTS.PAYMENT AS PAYMENT,
PAYMENTS.METHOD AS METHOD,
PAYMENTS.TOTAL AS TOTAL,
CUSTOMERS.NAME AS NAME,
(SELECT SUM(P.TOTAL) FROM PAYMENTS AS P
INNER JOIN RECEIPTS AS R ON P.RECEIPT = R.ID
INNER JOIN TICKETS AS T ON R.ID = T.ID
INNER JOIN CUSTOMERS AS C ON T.CUSTOMER = C.ID
WHERE C.ID = CUSTOMERS.ID and P.PAYMENT IN ('debt','debtpaid', 'advance', 'cashrefund')) AS CTOTAL
FROM RECEIPTS
INNER JOIN TICKETS ON RECEIPTS.ID = TICKETS.ID
INNER JOIN PAYMENTS ON RECEIPTS.ID = PAYMENTS.RECEIPT
INNER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID
WHERE
PAYMENTS.PAYMENT IN ('debt', 'debtpaid', 'advance', 'cashrefund')
....
....
WHERE -TOTAL > 0
VARIABLE
<variable name="DUES" class="java.lang.Double" resetGroup="Customer" resetType="Group" calculation="Nothing">
<variableExpression><![CDATA[$F{CTOTAL} + $F{TOTAL}]]></variableExpression>
<initialValueExpression><![CDATA[new Double(0.0)]]></initialValueExpression>
</variable>
OUTPUT
You can define the variable itself in SQL statement like this :-
SELECT SUM(#csum := #csum + TOTAL+PARTIAL)
FROM (SELECT TOTAL, PARTIAL,#csum := 0
FROM PRICE WHERE TOTAL > 0
) a;
See this question and answer
Related
I have a table named as aviationDispute which has 2 columns deliveryId type=aviationdelivery and invoiceId type=AviationB2BDocuments , now when we raise a dispute from post man it will be eith against a delivery or either against an invoice both the columns can never be filled in one dispute .
The main problem is : my flexi query is only working when both the columns are filled , if one is filled and one remains then it does not gives result .
My flexi query is below
select {p:pk} from {AviationDispute as p join AviationB2BDocuments as a on {p:invoiceId}={a:pk} join AviationDelivery as d on {p:deliveryId} = {d:pk}} where ({d:deliveryId} LIKE '%searchTerm%' or {a:invoiceNumber} LIKE '%searchTerm%')
I have tried various combos of is null and is not null with brackets but nothing is working
What you need is left join
select {p:pk} from {
AviationDispute as p
left join AviationB2BDocuments as a on {p:invoiceId}={a:pk}
left join AviationDelivery as d on {p:deliveryId} = {d:pk}
}
where
{d:deliveryId} LIKE '%searchTerm%' or {a:invoiceNumber} LIKE '%searchTerm%'
hope following query would help:
select {p:pk} from {AviationDispute as p join AviationB2BDocuments as a on {p:invoiceId}={a:pk} join AviationDelivery as d on {p:deliveryId} = {d:pk}} where {d:deliveryId} LIKE '%searchTerm%' and {a:invoiceNumber} is null or {a:invoiceNumber} LIKE '%searchTerm%'
deliveryId will never be null in the result, because of natural join
I am trying to reproduce this MySQL query in jooq
select case
when year(d.date) - p.birth_year < 5 then '<5'
else '5+'
end as age
from partners p join departure d on d.id = p.id
to
this.schema().select(
DSL.decode().value(dateDiff(p.BIRTHDATE , date(d.DATE)))
.when(greaterThan(5), "above 5")
.when(lessThan(5), "under 5")
.otherwise("unknown").as("age"),
.from(p)
.join(d).on(d.ID.eq(p.ID))
What you seem to be looking for is the SQL:2003 simple CASE expression, which allows to form something like "partial predicates" based on the case value, akin to pattern matching in other languages. This is not yet supported in jOOQ, see Issue #3223.
Interestingly, though, your SQL example doesn't use this syntax, nor does it correspond to your suggested jOOQ API usage. I suspect you wanted to use this syntax to avoid repeating the subtraction twice. This could be done also as follows, in SQL:
select
case sign(year(d.date) - p.birth_year - 5)
when -1 then '<5'
when 0 then '5+'
when 1 then '5+'
else 'unknown' end AS age
from partners p join departure d on d.id = p.id
This would translate to:
Partners p = PARTNERS.as("p");
Departure d = DEPARTURE.as("d");
using(configuration)
.select(choose(sign(year(d.DATE).minus(p.BIRTH_YEAR).minus(5)))
.when(inline(-1), val("<5"))
.when(inline( 0), val("5+"))
.when(inline( 1), val("5+"))
.otherwise(inline("unknown"))
.as("age"))
.from(p)
.join(d).on(d.ID.eq(p.ID))
.fetch();
This static import is implied:
import static org.jooq.impl.DSL.*;
Can you please share me code snippet to be written via JPA in order to generate the below sql query
SELECT COUNT(*) FROM Customer c
WHERE c.countryId ='Canada' AND
c.lanuguage ='ENG' AND
ROW_NUM <=10;
Because I tried in the below way. But MaxResults is not getting applied it seems as I can able to recieve the count more than 10.
Query query = em.createQuery("SELECT COUNT(c) FROM Customer c where c.countryId ='Canada' and c.lanuguage ='ENG'");
query.setMaxResults(10);
long customerCount = (Long)query.getSingleResult();
Select on count will always return a single value. If you want to have a count lower than 10, add HAVING.
SELECT COUNT(c) AS
FROM Customer c
WHERE c.countryId='Canada' and c.language='END'
HAVING COUNT(c)<=10
This is my sql command:
SELECT GIG.GIG_DESCRIPTION, VENUES.VENUE_NAME, BAND.BAND_NAME,
CASE WHEN GIG.USERID = 0 THEN '--CREATED BY THE BAND--' ELSE USERS.USERNAME END,
GIG.GIG_DATE
from GIG
INNER JOIN VENUES ON GIG.VENUEID = VENUES.VENUEID
INNER JOIN BAND ON GIG.BANDID = BAND.BANDID
INNER JOIN USERS ON GIG.USERID = USERS.USERID
WHERE GIG.USERID != 0
AND GIG.GIGID=" + gigID;
I'm using this query to return some values for a java object. In the Gig table sometimes the userid will equal 0, I'm getting a null pointer exception when I try and return a row with the userid equal to 0. I think I can get rid of the error if that last inner join on the users isn't run if a certain condition isn't true. Can I use a where clause that only effects the last join? How would I do that?
I think that you need link the USERS table using a LEFT JOIN clause in order to not discard the results with userid = 0 and remove the GIG.USERID != 0 on the WHERE clause:
SELECT GIG.GIG_DESCRIPTION, VENUES.VENUE_NAME, BAND.BAND_NAME,
CASE WHEN GIG.USERID = 0 THEN '--CREATED BY THE BAND--' ELSE
USERS.USERNAME END,
GIG.GIG_DATE
from GIG
INNER JOIN VENUES ON GIG.VENUEID = VENUES.VENUEID
INNER JOIN BAND ON GIG.BANDID = BAND.BANDID
LEFT JOIN USERS ON GIG.USERID = USERS.USERID
WHERE GIG.GIGID=" + gigID;
You can have multiple conditions in a join, try something like
INNER JOIN USERS ON GIG.USERID = USERS.USERID AND GIG.USERID != 0
WHERE ...
The where clause is applied to the result of the joins, so it can't be limited to just the last inner join.
You can add your condition to the where clause, of course. But you should keep in mind that it will affect the whole join.
I would suggest you add your condition to your on clause for the particular join.
I have a table with name test in Oracle, and (sal(integer),gender) are the two columns in it.
I want to get counter number where gender is male, sal betwwen 1000,3000.
For example: If I have three people in table test (two male ,one female) and person1 sal =1000, person2 sal = 2020, person3 sal = 1040
The return value of sql statement will be equals (2).
The variable will store return value from java.
you actually want to count rows not column, as per what i understood from ur question. then you can use count(column_name) function of SQL along with the conditions (where clause).
Query: SELECT COUNT(*) "Count" FROM test WHERE gender='male' AND salary BETWEEN 1000 AND 3000
try this query,
select count(*) from <table_name> where gender='male' and salary between 1000 to 3000;