coalesce mybatis switch case - java

Iam using coalesce mybatis switch case in my query, where iam getting error like
Error querying database. Cause: java.sql.SQLException: ORA-01427:
single-row subquery returns more than one row
this is my query
(select
(case when (coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null)
then (select sysdate from dual)
else (coalesce(t1.col1,t2.col1, t1.col2, t1.col3))
end )
from table1 t1
join table2 t2
on t1.id IN (t2.id))
Thanks in advance

Seems you have a lot of () but overall you should use = operator and not IN (t2.id) for join t2.id
select
case when coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null
then sysdate
else coalesce(t1.col1,t2.col1, t1.col2, t1.col3)
end
from table1 t1
join table2 t2 on t1.id = t2.id
And looking at the code you posted in sample you have a select as a column result and this select return several rows, ( this raise the error). You also have a mixin of join syntax some based on explicit join syntax some based on old implicit join syntax based on comma separated table name and where condition. You should try using this
<select id="Trigger" parameterType="hashmap" resultType="java.util.HashMap" flushCache="true">
SELECT
select case when coalesce(table1.col1, table2.col2,table1.col3, table1.col4) is null
then sysdate
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4) end as "ProgressDate"
, table3.id as "ID"
from table1
INNER join table2 on table1.id = table2.id
INNER JOIN table3 ON table1.id = table3.id
INNER JOIN table4 table2.action = table4.action
WHERE table3.transaction = #{inputvaluepassed}
</select>

The query you mention in the question takes the place of a scalar subquery included in another... main query. I formatted the whole query (for readability) and it looks like this:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
Now, by definition, scalar subqueries can only return zero or one row. In your case it seems that at runtime this subquery is returning multiple rows, and the main query crashes.
You'll need to somehow produce a single row at most: maybe by aggregating the rows (using GROUP BY), maybe by picking one row only from the result set (using LIMIT); there are other options. If we choose the to limit the rows to 1 at most your query could look like:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
limit 1 -- added this line
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
This is just one possible cheap solution to the issue. A better understanding on how to pick the right row over multiples ones can produce a better solution.

Related

CriteriaBuilder. How to represent a JOIN of one table by two foreign keys with OR condition through CriteriaBuilder?

I have, for example, two tables:
Table1:
id
name
Table2:
id
title
parent_id (FK to table1)
child_id (FK to table1)
How to repsent a query like this through CriteriaBuilder:
SELECT * FROM table1 t1
LEFT JOIN table2 t2 ON (t2.parent_id = t1.id OR t2.child_id = t1.id)
WHERE t2.title = {title}
I tried this code, but it didn't help. First condition overwrites the second one
Root<Table1> table1 = cq.from(Table1.class);
Join<Table1, Table2> table2 = table2.join(Table1_.parent, JoinType.LEFT);
table2.on(cb.or(cd.equal(table2.get(Table2_.id), Table1_.parent.id), cd.equal(table2.get(Table2_.id), Table1_.child.id));
...

JQPL select inside select

just switched to spring boot from .NET Core, in .NET core we can easily nest a select inside a select like this:
var result = from c in context.Cars
join br in context.Brands
on c.BrandId equals br.Id
join col in context.Colors
on c.ColorId equals col.Id
select new CarDetailDto
{
Id = c.Id,
BrandName = br.Name,
CarName = c.Name,
ColorName = col.Name,
DailyPrice = c.DailyPrice,
ModelYear = c.ModelYear,
CarImages = (from cimg in context.CarImages
where cimg.CarId == c.Id
select new CarImage
{
Id = cimg.Id,
ImagePath = cimg.ImagePath,
CarId = c.Id,
Date = cimg.Date
}).ToList()
};
I want to do that in JPQL as well but didnt manage to solve
#Query( select column1, column2, column3 from tablename1 where coluname=(select columname from tablename2 where columnname=abcd) )
Your JPQL query should look like above.
Whatever subquery you write with condition.
If your query is fetching 3 column you need to create a DTO with same column name.
If your query is fetching list of rows then your actual jpql will look like this.
#Query( select column1, column2, column3 from tablename1 where coluname=
(select columname from tablename2 where columnname=abcd) )
List<ResultDTO> findAllResultList(Parameter value);
Above it is mapping the result to list of DTO objects to result rows.
If your query is fetching single row then your actual jpql will look like this.
#Query( select column1, column2, column3 from tablename1 where coluname=
(select columname from tablename2 where columnname=abcd) )
ResultDTO findResult(Parameter value);
The single result is mapped to one DTO object.
Make sure your result column name and DTO column name matches
Using the JPA repository call the names of the method which you used for the particular query.

remove duplicate Data after Execute SelectQuery in mutliple Tables

i have Three tables [users,projects,scenarios] i need to get latest update projects details based on modified Date column with out duplicate values
the tables are:
users Table :
project table
scenario Table
and i try below query but its return duplicates values if am using group by then old values came but i need latest values
With out Group by query
SELECT
p.`PROJECT_NAME`,
p.`CREATED_DATE`,
s.`MODIFIED_DATE`
FROM
`projects` p
JOIN `scenarios` s ON
s.`PROJECT_ID` = p.`PROJECT_ID`
WHERE
P.`USER_ID` =(
SELECT
USER_ID
FROM
users
WHERE
EMAIL = 'test#gmail.com'
)
ORDER BY
s.`MODIFIED_DATE`
DESC
out put:
with Group by Query :
SELECT
p.`PROJECT_NAME`,
p.`CREATED_DATE`,
s.`MODIFIED_DATE`
FROM
`projects` p
JOIN `scenarios` s ON
s.`PROJECT_ID` = p.`PROJECT_ID`
WHERE
P.`USER_ID` =(
SELECT
USER_ID
FROM
users
WHERE
EMAIL = 'test#gmail.com'
)
group by p.PROJECT_NAME
ORDER BY
s.`MODIFIED_DATE`
DESC
output:
You can separately get the latest scenario per project using a subquery then the resulting rows will then be join again to get the other columns, if needed.
SELECT p.PROJECT_NAME,
p.CREATED_DATE,
s.MODIFIED_DATE
-- all columns in s.* will have the latest row
FROM projects p
INNER JOIN scenarios s
ON p.PROJECT_ID = s.PROJECT_ID
INNER JOIN
(
SELECT project_ID, MAX(modified_date) MAX_modified_date
FROM scenarios
GROUP BY project_ID
) t ON s.project_ID = t.project_ID
AND s.modified_date = t.MAX_modified_date
INNER JOIN users u
ON P.USER_ID = u.USER_ID
WHERE u.EMAIL = 'test#gmail.com'
ORDER BY s.MODIFIED_DATE DESC
However, if you don't need to get the other columns, you can directly use MAX() and GROUP BY.
SELECT p.PROJECT_NAME,
p.CREATED_DATE,
MAX(s.MODIFIED_DATE) AS MAX_MODIFIED_DATE
FROM projects p
INNER JOIN scenarios s
ON p.PROJECT_ID = s.PROJECT_ID
INNER JOIN users u
ON P.USER_ID = u.USER_ID
WHERE u.EMAIL = 'test#gmail.com'
GROUP BY p.PROJECT_NAME,
p.CREATED_DATE
ORDER BY MAX_MODIFIED_DATE DESC

db2 sqlstate 42972 error

I am getting this error when I am trying to execute this sql query
INSERT INTO
AGG_QUALITY
SELECT
QIF.DATEDM_ID,
'Status' AS BREAKDOWN_TYPE,
HCR.VALUE,
QIF.SCANDEFINITION_ID,
QIF.QUALITYISSUE_VALUE,
COUNT(QIF.QUALITYISSUEFACT_ID),
COUNT(QRF.QUALITYRESOLUTIONFACT_ID)
FROM
QUALITYISSUEFACT QIF,
HUBCODERECORD HCR,
ITEMSTATUS ITS
LEFT JOIN
QUALITYRESOLUTIONFACT QRF
ON
QIF.QUALITYISSUEFACT_ID = QRF.QUALITYISSUEFACT_ID
WHERE
QIF.DATEDM_ID > startDateDMId
AND QIF.DATEDM_ID <= endDateDMId
AND HCR.CODE = ITS.H_STATUS_TYPE
AND QIF.DIMENSION_ROM_PK = ITS.ITEMMASTER_ID
GROUP BY
QIF.DATEDM_ID, HCR.VALUE, QIF.SCANDEFINITION_ID, QIF.QUALITYISSUE_VALUE
;
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0338N An ON clause associated with a JOIN operator or in a MERGE statement
is not valid. LINE NUMBER=31. SQLSTATE=42972
Try like below with inner joins:
INSERT INTO
AGG_QUALITY
SELECT
QIF.DATEDM_ID,
'Status' AS BREAKDOWN_TYPE,
HCR.VALUE,
QIF.SCANDEFINITION_ID,
QIF.QUALITYISSUE_VALUE,
COUNT(QIF.QUALITYISSUEFACT_ID),
COUNT(QRF.QUALITYRESOLUTIONFACT_ID)
FROM
QUALITYISSUEFACT QIF
INNER JOIN
HUBCODERECORD HCR
ON QIF.DIMENSION_ROM_PK = ITS.ITEMMASTER_ID
INNER JOIN
ITEMSTATUS ITS
ON QIF.DIMENSION_ROM_PK = ITS.ITEMMASTER_ID
LEFT JOIN
QUALITYRESOLUTIONFACT QRF
ON
QIF.QUALITYISSUEFACT_ID = QRF.QUALITYISSUEFACT_ID
WHERE
QIF.DATEDM_ID > startDateDMId
AND QIF.DATEDM_ID <= endDateDMId
GROUP BY
QIF.DATEDM_ID, HCR.VALUE, QIF.SCANDEFINITION_ID, QIF.QUALITYISSUE_VALUE
;
see if it helps
Had the same error here. (note, my Table1 has NO rows)
On my side I could solve it by changing the additional (comma separated) table2 to a REAL inner join.
Example from
select xxx from Table1 t1, Table2 t2
where (t2.col1 = t1.col1) and (t1.col2 = "whatever")
to
select xxx from Table1 t1
INNER JOIN Table2 t2 ON (t2.col1 = t1.col1)
where (t1.col2 = "whatever")
Hope this helps someone else.
(problem occured on DB2 9.7)

Cryptic MSSQL error message

Previous related thread: Join in Query WHERE clause
I'm getting this error:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '.'.
My SQL Statement:
SELECT products.id, products.name products.extended_description, products.catalogid, products.image1, products.image2, products.stock, products.price, manufacturer.manufacturer, products.weight
FROM products
JOIN manufacturer ON (products.manufacturer = manufacturer.id)
JOIN product_category ON (product_category.catalogid = products.catalogid)
JOIN category ON (category.id = product_category.id)
WHERE category.category_name = ?;
What do I have wrong here? My statement looks correct to me...
You are missing a comma between products.name and products.extended_description. It thinks that products.extended_description is the alias. If it is then put []s around it. [products.extended_description]. Otherwise put in the missing comma.
SELECT products.id, products.name, products.extended_description, products.catalogid,
products.image1, products.image2, products.stock, products.price,
manufacturer.manufacturer, products.weight
FROM products
JOIN manufacturer ON (products.manufacturer = manufacturer.id)
JOIN product_category ON (product_category.catalogid = product.catalogid)
JOIN category ON (category.id = product_category.id)
WHERE category.category_name = ?;
You forgot a comma after products.name
Should be:
SELECT products.id, products.name, products.extended_description, ...

Categories

Resources