java.sql.SQLException: Invalid use of group function - java

From this query:
select count(*) as data, LOCATION_Name as location
from SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
where sno>=6973
and sno<= 7251
and date(PAYMENT_DATE) = '2021-07-31'
and Procedures_Category IN ('FFA-VR')
and SUM(BILLED_AMOUNT)= 0
group by LOCATION_Name
I am getting this error:
java.sql.SQLException: Invalid use of group function
What is the issue?

You cannot use an aggregate function in the where clause. Kindly change yor query to:
select count(*) as data, LOCATION_Name as location
from SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
where sno>=6973 and sno<= 7251 and
date(PAYMENT_DATE) = '2021-07-31' and Procedures_Category IN ('FFA-VR')
group by LOCATION_Name
Having SUM(BILLED_AMOUNT)= 0

The restriction on the sum of the billed amount belongs in the HAVING clause, rather than the WHERE clause:
SELECT COUNT(*) AS data, LOCATION_Name AS location
FROM SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
WHERE sno BETWEEN 6973 AND 7251 AND
PAYMENT_DATE >= '2021-07-31' AND PAYMENT_DATE < '2021-08-01' AND
Procedures_Category = 'FFA-VR'
GROUP BY LOCATION_Name
HAVING SUM(BILLED_AMOUNT) = 0;

Related

Incorrect Syntax near WHERE error in generated SQL

I am generating the below SQL. From my code I am using a where condition list to collect all the Where logic and insert it after the Join logic is set-up. However, I am getting a very generic syntax error and I can't figure out why. I am pretty sure the logic is properly organized however, when inserting the where statement it throws the syntax error
Incorrect Syntax near WHERE
The {h-schema} are just generated database and table names.
The code:
SELECT count(*) AS ID
FROM (
SELECT 'PREAPPROVAL' AS type, pa.id AS id FROM {h-schema}preapproval AS pa
LEFT JOIN {h-schema}risk_limit AS lim ON pa.limit_id = lim.id
LEFT JOIN {h-schema}desk AS d ON lim.desk_enterprise_id = d.enterprise_id AND CAST(pa.creation_date_time AS date) BETWEEN d.start_date AND d.end_date
WHERE pa.status = 'APPROVED' AND pd.end_date = NULL <-------------------------SYNTAX ERR HERE
OR pa.status = 'DECLINED' AND pa.completion_date_time > '2021-01-28 13:02:13'
OR pa.status = 'IN_PROGRESS'
OR pa.creation_date_time > '2021-01-28 13:02:13'
AND COALESCE(lim.policy_enterprise_id, d.policy_enterprise_id) IN (6)
UNION
SELECT 'BREACH' AS type, br.id AS id FROM {h-schema}breach AS br
LEFT JOIN {h-schema}risk_limit AS lim ON br.limit_id = lim.id
LEFT JOIN {h-schema}desk AS d ON lim.desk_enterprise_id = d.enterprise_id
AND br.reporting_date BETWEEN d.start_date AND d.end_date
LEFT JOIN {h-schema}valid_breach_recommendation AS vbr_approve ON vbr_approve.id = (SELECT TOP(1) id FROM {h-schema}valid_breach_recommendation
WHERE breach_id = br.id AND outcome = 'APPROVE'
ORDER BY creation_date_time DESC, id DESC)
LEFT JOIN {h-schema}valid_breach_decision AS vbd
ON vbd.id = (SELECT TOP(1) id FROM {h-schema}valid_breach_decision
WHERE breach_id = br.id
ORDER BY creation_date_time DESC, id DESC)
LEFT JOIN {h-schema}breach AS child_br ON br.id = child_br.parent_breach_id
WHERE br.status = 'APPROVED' AND vbd.end_date = NULL <--------------------SYNTAX ERR HERE
OR br.status = 'DECLINED' AND br.completion_date_time > '2021-01-28 13:02:14'
OR br.status = 'IN_PROGRESS'
OR br.creation_date_time > '2021-01-28 13:02:14'
AND child_br.id IS NULL
AND CASE br.status
WHEN 'IN_PROGRESS' THEN vbr_approve.start_date
WHEN 'APPROVED' THEN vbd.start_date
WHEN 'CANCELLED' THEN vbd.start_date
ELSE NULL
END IS NOT NULL AND COALESCE(lim.policy_enterprise_id, d.policy_enterprise_id) IN (6)
) AS issue
This is actually version issue. In latest version of SQL support like
create procedure sp_name
(
#name varchar(50) NULL
)
...
But the older version of SQL doesn't support with this way. For older version we need to provide '=NULL'
There is some syntax error due to {h-schema}, remove {h-schema} and your code has no syntax error. Remove {h-schema} and your code works fine.

could not read column value from result set in Hibernate Native Query

SELECT
CONCAT(CREG.FIRSTNAME, ' ', CREG.LASTNAME) AS NAME,
CASE WHEN CR.CAMPAIGNTYPE = 'NPS'
THEN NPSSCORE
ELSE CSATSCORE END AS SCORE,
IFNULL(cast(CC.TEXT AS CHAR(255)), '') AS COMMENTS,
CREG.ID AS CLIENTID,
CR.ID AS CAMPAIGNRESPONSEID,
CI.ID AS ISSUEID
FROM CUSTOMER_ISSUES CI INNER JOIN (SELECT DISTINCT ISSUEID
FROM ISSUE_DEPARTMENT_MAPPING
WHERE CUSTOMERUSERID = 91 AND ISSUE_STATUS = 'New') IDM
ON CAST(CI.FEEDBACK_DATE AS DATE) BETWEEN '2016-06-05' AND '2016-06-11' AND IDM.ISSUEID = CI.ID
INNER JOIN CAMPAIGN_RESPONSE CR ON CR.ID = CI.CAMPAIGN_RESPONSE_ID
INNER JOIN CLIENT_REGISTRATION CREG ON CREG.ID = CR.RESPONSECUSTOMERID
LEFT OUTER JOIN CAMPAIGN_COMMENTS CC ON CC.CAMPAIGN_RESPONSE_ID = CR.ID;
The above query is running in the mysql-console properly ,but when I am integrating with the Hibernate,following error is thrown by Hibernate.
[BigIntegerType] could not read column value from result set: ID; Column 'ID' not found.
Try getting rid of the alias names in your SQL query.
Basically what happens here is when you run
SELECT
CONCAT(CREG.FIRSTNAME, ' ', CREG.LASTNAME) AS NAME,
CASE WHEN CR.CAMPAIGNTYPE = 'NPS'
THEN NPSSCORE
ELSE CSATSCORE END AS SCORE,
IFNULL(cast(CC.TEXT AS CHAR(255)), '') AS COMMENTS,
CREG.ID AS CLIENTID,
CR.ID AS CAMPAIGNRESPONSEID,
CI.ID AS ISSUEID
in JDBC it returns the column as CREG.ID instead of ClientID.
So try running the query without the aliases, typically, there is a problem in JDBC with this. If you still insist on using aliases,add the following entry to JDBC URL in configuration file
[useOldAliasMetadataBehavior=true]

Maximo setWhere() query to get distinct results?

I'm trying to do this SQL query:
select distinct wonum from invreserve where location='01' order by wonum;
How can this be converted to a setWhere() query? I've tried:
invreserveSet.setWhere("(1 = 1) and wonum in (select distinct wonum from invreserve where location='01')");
work with something like this:
select invreserveid from invreserve where invreserveid in (select invreserveid from invreserve invr2 where location like '%B%'
and invreserveid = (select max(invreserveid) from invreserve invr3 where invr3.location = invr2.location and invr3.wonum = invr2.wonum))
;
so your where clause would be the same as this ones except your location wildcard would be different.

Mysql - result the data's with or with out available in DB

This is my sql query to sum the error count in DB. What made the problem below is, when I check the available data in this, it retrieve the actual and correct data given, if I'm giving any wrong (that is unavailable data in table) input it gives nothing that is it doesn't throw any output screen:
SELECT
SUM(IF(ieb.errortype_id='1',1,0)) AS Total_Critical,
SUM(IF(ieb.errortype_id='2',1,0)) AS Total_Non_critical,
u.emp_id,
u.emp_name
FROM
issueerror_empmap iem,
issueerror_body ieb,
USER u,
issueerror_type iet
WHERE
ieb.chapter_id = '424262' AND
iem.chapter_errorid=ieb.chapter_errorid AND
iem.emp_id = u.emp_id AND
u.emp_id = '693' AND
ieb.errortype_id=iet.errortype_id
GROUP BY
iem.emp_id,
u.emp_name
ORDER BY
iem.emp_id
Can any one please suggest me how to build this query to retrieve the data if available and display the output as null if the input data not available in a table.
For producing the output for the above query is (if data available in a table):
Total_Critical Total_Non_Critical emp_id emp_name
4 10 xxx xxx
For producing the output for the above query is (if data not available in a table):
Total_Critical Total_Non_Critical emp_id emp_name
I am expecting the emp_id and emp_name with the error count as null or zeroes.
how about this query?
select
sum (Total_Critical) as Total_Critical,
sum(Total_Non_critical) as Total_Non_critical,
emp_id,
emp_name from
(
(SELECT
SUM(IF(ieb.errortype_id='1',1,0)) AS Total_Critical,
SUM(IF(ieb.errortype_id='2',1,0)) AS Total_Non_critical,
u.emp_id,
u.emp_name
FROM
issueerror_empmap iem,
issueerror_body ieb,
USER u,
issueerror_type iet
WHERE
ieb.chapter_id = '424262' AND
iem.chapter_errorid=ieb.chapter_errorid AND
iem.emp_id = u.emp_id AND
u.emp_id = '693' AND
ieb.errortype_id=iet.errortype_id
GROUP BY
iem.emp_id,
u.emp_name
ORDER BY
iem.emp_id )
) UNION ALL (
select 0,0,null, null
)
) as tbl1

OpenJPA: how to construct a GROUP BY query with a group count

In JPQL I want to construct the equivalent query to this:
select *, count(*) as finger_count from page_delta_summary
where delta_history_id = ? and change_type = ? group by fingerprint;
where fingerprint is a varchar field in table page_delta_summary. What I have is this:
select d, count(d) as finger_count from PageDeltaSummary d
where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType
GROUP BY d.fingerprint"
where PageDeltaSummary is my entity. But I'm getting the following exception:
org.apache.openjpa.persistence.ArgumentException: Your query on type "class com.su3analytics.sitedelta.model.PageDeltaSummary" with filter "select d, count(d) from PageDeltaSummary d where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType GROUP BY d.fingerprint" is invalid. Your select and having clauses must only include aggregates or values that also appear in your grouping clause.
The query works fine if I remove either count(d) as finger_count or the GROUP BY.
Any suggestions?
Thanks
Your original SQL query doesn't make sense, therefore you can't convert in into JPQL.
I guess you want to get count of page_delta_summary rows satisfying where conditions for each fingerprint. If so, the SQL query looks like this:
select fingerprint, count(*) as finger_count from page_delta_summary
where delta_history_id = ? and change_type = ? group by fingerprint;
and JPQL - like this:
select d.fingerprint, count(d) from PageDeltaSummary d
where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType
GROUP BY d.fingerprint
These queries return pairs <fingerprint, finger_count> instead of full page_delta_summary rows (or entities).

Categories