how to use order by in sql select query to sort complaint no in ascending order - sql-order-by

qry1 = "Select * from ser_complaint_master a,ser_complaint_status b,company_master c
where a.complaint_no=b.complaint_no
and a.allocation_code=c.co_code
and c.co_br_code='" + Session["BRCODE"] + "'
and a.Complaint_Date>='" + Frdat + "' and a.Complaint_Date<='" + Todat + "'
and a.status in ('Completed')
and a.complaint_type in('" + cmptype + "')";
How to use ORDER BY in select query if more than one tables are involved.

Add order by a.complaint_no to the end of the query.

Related

MY SQL SYNTAX ERROR HOW TO FIX IN SPRING BOOT

Getting ERROR: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near emp.employeeMangement.api.DTO.ResponseDTO(emp.empName,emp.gender,emp.email,emp at line 1
#Query(value = "select NEW com.emp.employeeMangement.api.DTO.ResponseDTO(" +
`"emp.empName," +
"emp.gender," +
"emp.email," +
"emp.empCode," +
"att.noOfAbsent," +
"att.noOfPresent," +
"att.month," +
"att.noOfDaysInMonth," +
"sal.salAmount," +
"round(#per_day\\:=sal.salAmount/att.noOfDaysInMonth) as salperday," +
"round(att.noOfPresent*#per_day) as Salarycurrentmonth " +
") " +
"from Employee as emp " +
"join Attendence as att " +
"on emp.emp_id=att.cp_fk" +
" left join Salary as sal " +
"on sal.emp_id_fk=emp.emp_id",nativeQuery = true)
List<ResponseDTO> getInfoInExcel();

Equivalent of Select #variable in Spring Data Jpa

I need to run a long query on a spring data jpa method call.
What is the equivalent of Select #sql in spring jpa.
I am getting exception when i try to execute it as a native query.
Below is the query that is working fine in SQL server
Declare #sql varchar(max) = 'select '
select #sql = #sql + 'sum(' + c.name + ')' + ' AS ' + c.name + ','
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
inner join sys.types ty on
c.user_type_id = ty.user_type_id
where
ty.name in ('bigint') and
t.name = 'my_used_features'
select #sql = LEFT(#sql,LEN(#sql)-1) + ' from my_used_features '
EXEC (#sql)
I want to write the same query as native query in my repository class.But I am getting exception like ....... EXEC (#sql)> starts a quoted range at 342, but never ends it.I am not able to figure out the issue.Can anyone please help me on what am I doing wrong.
#Query
(value="Declare #sql varchar(max) = 'select '\r\n"
+ " \r\n"
+ " select #sql = #sql + 'sum(' + c.name + ')' + ' AS ' + c.name + ',' \r\n"
+ " from sys.columns c\r\n"
+ " inner join sys.tables t on c.object_id = t.object_id\r\n"
+ " inner join sys.types ty on\r\n"
+ " c.user_type_id = ty.user_type_id\r\n"
+ " where\r\n"
+ " ty.name in ('bigint') and\r\n"
+ " t.name = 'my_used_features'\r\n"
+ " \r\n"
+ " select #sql = LEFT(#sql,LEN(#sql)-1) + ' from my_used_features ' \r\n"
+ " \r\n"
+ " EXEC (#sql)",nativeQuery=true)
Map<String,Integer> findSum();

linq condition in select statement

I have a linq query
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + c.MiddleName);
The MiddleName could be NULL, how do I replace that null with a space or ignore it?
If I leave it this way, the query does not return any records for customers who don't have middle names.
You can do as such:
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + (c.MiddleName ?? "");
This should do the trick :)

jpql native query not setting parameter

#Repository
public interface GroupRepository extends JpaRepository<Group, String> {
//Other queries....
#Query(value = "with cte(group_id, parent_group_id, group_name) as( "
+ "select group_id, parent_group_id, group_name "
+ "from hea.hea_group "
+ "where group_id = ?1 "
+ "union all "
+ "select g.group_id, g.parent_group_id, g.group_name "
+ "from hea.hea_group g "
+ "inner join cte on cte.group_id = g.parent_group_id "
+ "where g.parent_group_id is not null "
+ ") select * from cte", nativeQuery = true)
List<Object> getChildGroups(String groupId);
}
Above is the query that I have written that should return the parent group and all of its children. The query does what it is suppose to do when I replace the ?1 with a hard coded group id value and change the method to have no parameters, but when I try to run it as above it returns nothing even though I'm passing in the exact same value that I was hard coding.
Below is the sql that is being generated by the query. When I replace the ? with a group id an run it on a test database it returns the results that it should.
with cte(group_id, parent_group_id, group_name) as( select
group_id,
parent_group_id,
group_name
from
hea.hea_group
where
group_id = ?
union
all select
g.group_id,
g.parent_group_id,
g.group_name
from
hea.hea_group g
inner join
cte
on cte.group_id = g.parent_group_id
where
g.parent_group_id is not null ) select
*
from
cte
The variables are zero based so ?0 is what you should use.

JPA and Hibernate NamedQuery Error: maximum number of expressions in a list is 1000

I have the below Hibernate NamedQuery that runs into issues when one of the "in" expressions has 1000 or more items. When I have 1000 or more items in the ma.deviceId in (:devices), I get java.sql.SQLException: ORA-01795: maximum number of expressions in a list is 1000
The only "in" expression that I need to take care of is the "and ma.deviceId in (:devices)". Anyone have any ideas on how to rewrite this NamedQuery? I'm using JPA and Hibernate.
#NamedQuery(name = "Messages.findMessages", query = " SELECT ma from Messages ma JOIN FETCH ma.messagePayLoadXml mx LEFT OUTER JOIN FETCH ma.messageProperties mp " +
" WHERE ma.customerId = :customerId and ma.time >= :startTime and ma.time <= :endTime " +
" and ma.deviceId in (:devices) and mx.messageType = 'XML' and mx.alerts in " +
" ( select mtfm.messageType from MessageTypeFeatureMap mtfm where mtfm.feature in (:featureType) ) " +
" and ma.messageKey = mx.messageKey and ( mp.deleted = 0 or mp.deleted is null ) " +
" order by ma.time desc " )
There are 2 ways.
1) Store your list in intermediary table and do
... IN (SELECT ... FROM intermediaryTable)
2) Break your list into sublists each upto 1000 elements and write your query as
(... IN (subList1) OR ... IN (subList2) ...)
For our application we have done a simple fix for this situation...
If number of values are greater than 999
QUERY = SELECT + FROM + WHERE + COND1 +
( FIELD IN ( 999 values ) OR FIELD IN ( 999 values )...) + ORDER
Else
QUERY = SELECT + FROM + WHERE + COND1 + IN (...) + ORDER

Resources