objectdb select where query - where-clause

I attempt to select some instance using where clause
public static List<RSSItem> getRSSItem(int x1, int x2) {
EntityManagerFactory emf = DBHandler.getEmf();
EntityManager em = DBHandler.getEm(emf);
String query =
"SELECT items FROM RSSItem items "
+ "WHERE items.id <= :x1 AND "
+ "items.id >= :x2";
List<RSSItem> results =
(List<RSSItem>) em.createQuery(query).
setParameter("x1", x1).
setParameter("x2", x2).
getResultList();
return results;
}
the RSSItem attributes :
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
String title;
String link;
String description;
String pubdate;
String content;
HashMap<String, Integer> keyword = new HashMap();
HashMap<String, Integer> keywordBefore = new HashMap();
// TreeMap <String, Integer> keyword = new TreeMap();
String feed;
the problem is it always return a list with 0 size. what's wrong with my select query?

With the values x1 = 1 and x2 = 500, the query turns into...
SELECT items FROM RSSItem items
WHERE items.id <= 1
AND items.id >= 500
Since no id's are less or equal to 1 and greater or equal to 500 at the same time, the query will give no hits. What you want is probably;
String query =
"SELECT items FROM RSSItem items "
+ "WHERE items.id >= :x1 AND "
+ "items.id <= :x2";
...which with your example data will find all id's between 1 and 500, inclusive.

Related

how to get only first row of datatable with modified specific row value

I have query to just summary of total no of jobs running. now I just want some specific result if there is unique rows found like unique category id with assign job then ok with multiple record set. but no category found if is null then just only pass first record from datatable with modified text with 'ALL' as category name. can we achieve this result.
here is my query and some operations I'm doing with them.
string str = "";
DataTable dt = new DataTable();
str = "SELECT j.[JobID], p.[Id] As PreparedEmailID,p.[Title] AS 'PreparedEmailName',j.[CreatedOn],j.[CompletedOn],j.CategoryID,j.[SubscriberCount],j.[EmailsSent],c.[CategoryName] As SubscriberCategory,(SELECT TOP 1 [Message] FROM [LoggedMessages] WHERE [JobID] =j.[JobID] ORDER BY [LoggedMessageID] DESC) AS 'LoggedMessage',(SELECT [Name] FROM tbl_User_master u WHERE u.Id =j.UserID) As CreatedBy FROM [Jobs] AS j INNER JOIN [tbl_Email_master] AS p ON p.[Id] = j.[PreparedEmailID] INNER JOIN [tbl_User_master] AS u ON u.[Id]=j.[UserID] INNER JOIN tbl_Categories c ON c.Id = j.CategoryID OR (c.Id IS NOT NULL AND j.CategoryID IS NULL) where 1=1 ";
if (chk_date.Checked == true)
{
str += " and ( [CreatedOn] between '" + CommonLogic.Get_Date_From_String(txt_date_from.Text, 1);
str += "' and '" + CommonLogic.Get_Date_From_String(txt_date_to.Text, 2) + "' )";
}
if (string.IsNullOrEmpty(txttitle.Text.Trim()))
{
str += string.Empty;
}
else
{
str += " and p.Title like '%" + txttitle.Text.Trim() + "%'";
}
if (ddl_fromuser.SelectedItem.Text.ToString() == ".All")
{
str += string.Empty;
}
else
{
str += " and j.FromuserID = CONVERT(INT," + Convert.ToInt32(ddl_fromuser.SelectedValue.ToString()) + ")";
}
if (ddl_subcategories.SelectedItem.Text.ToString() == ".All")
{
str += string.Empty;
}
else
{
str += " and j.CategoryID = CONVERT(INT," + Convert.ToInt32(ddl_subcategories.SelectedValue.ToString()) + ")";
}
dt = obj.Get_Data_Table_From_Str(str);
if (dt.Rows.Count > 1)
{
dt.Rows[0]["SubscriberCategory"] = "ALL";
var topRows = dt.AsEnumerable().FirstOrDefault();
egrd.DataSource = topRows;
egrd.DataBind();
}
else
{
egrd.DataSource = dt;
egrd.DataBind();
}
ViewState["data"] = dt;
how ever this gives me error like no JobID found to this record set. whether it is still exists in record set.
please help me...
well I tried this solution but no success...……..
if (dt.Rows.Count > 1)
{
dt.Rows[0]["SubscriberCategory"] = "ALL";
var topRows = dt.AsEnumerable().GroupBy(j => j.Field<int>("JobID")).Select(j => j.First()).ToList();
egrd.DataSource = topRows;
egrd.DataBind();
}
it's gives me exception like DataBinding: 'System.Data.DataRow' does not contain a property with the name 'JobID'.
Just replace .ToList() with .CopyToDataTable() resolve my problem

find ordered list in hql by max different between values with max dates

I have a table in postgres
CREATE TABLE employer_visit(
employer_id INTEGER REFERENCES employer,
visit_counter INTEGER NOT NULL, -- counter on current date
visit_before_date_total_counter INTEGER NOT NULL, -- total, since service has started
date DATE,
PRIMARY KEY(employer_id, date)
);
I need to find top employers by visits from last 30 days - (ordered list of employer_visit by diff between visit_before_date_total_counter in last record and last record 30 days ago)
I have tried this hql query
Query<EmployerVisit> query = getSession().createQuery(
"SELECT ev FROM EmployerVisit ev " +
"WHERE ev.employerVisitId.date = (SELECT MAX (groupedEv.employerVisitId.date) FROM EmployerVisit groupedEv " +
"WHERE groupedEv.employerVisitId.employerId = ev.employerVisitId.employerId) " +
"AND " +
"ev.employerVisitId.date = (SELECT MAX (groupedEv2.employerVisitId.date) < :date FROM EmployerVisit groupedEv2 " +
"WHERE groupedEv2.employerVisitId.employerId = ev.employerVisitId.employerId) " +
"ORDER BY (groupedEv.visitCounter + groupedEv.visitBeforeDateTotalCounter) - (groupedEv2.visitCounter + groupedEv2.visitBeforeDateTotalCounter) DESC"
).setMaxResults(size).setParameter("date", calendar.getTime());
But it falls with
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'groupedEv.visitCounter'
Becouse the table groupedEv doesn't exist after ORDER BY
It is possible to use depricated SqlQuery for this case in hibernate but I prefere hql, please help, thanks.
UPDATE! - Entity and EmbeddedId Class without setters and getters
#Entity
#Table(name = "employer_visit")
public class EmployerVisit {
#EmbeddedId
private EmployerVisitId employerVisitId;
#Column(name = "visit_counter")
private Integer visitCounter;
#Column(name = "visit_before_date_total_counter")
private Integer visitBeforeDateTotalCounter;
public EmployerVisit() {
employerVisitId = new EmployerVisitId();
visitCounter = 0;
visitBeforeDateTotalCounter = 0;
}
}
#Embeddable
public class EmployerVisitId implements Serializable {
#Column(name = "employer_id")
private Integer employerId;
#Column(name = "date")
private Date date;
public EmployerVisitId() {
}
}
This is the Answer
Query<Object> query = getSession().createQuery(
"SELECT ev, SUM(ev.visitCounter) AS counter FROM EmployerVisit AS ev " +
"WHERE ev.employerVisitId.date > :date " +
"GROUP BY ev.employerVisitId.employerId, ev.employerVisitId.date " +
"ORDER BY counter DESC").setMaxResults(size).setParameter("date", calendar.getTime());
List<EmployerVisitDto> top = new ArrayList<>();
for (Object result : query.list()) {
Object[] dividedResult = (Object[]) result;
EmployerVisit employerVisit = (EmployerVisit) dividedResult[0];
EmployerVisitDto employerVisitDto = getEmployer(employerVisit.getEmployerId()).toEmployerVisitDto();
employerVisitDto.setPeopleVisited(((Long) dividedResult[1]).intValue());
top.add(employerVisitDto);
}
return top;

Spring query where in () is not working

I am trying to query faculties that have 3 subjects mysql statement is as the following
select * from faculty_subject where subject_id in(1 and 3 and 2);
where 1, 2, 3 are subject ids. I only want courses that have all these subjects so that reason I am using and. But I do not why my spring query is not working. I tried different ways but it's returning faculties with the first subject id in this case 1. Here is my spring query
#Query(value = "select * from faculty_subject where subject_id in(?#{[0]} and ?#{[1]} and ?#{[1]})", nativeQuery = true)
List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);
another one that did not work
#Query(value = "select * from faculty_subject where subject_id in(?1 and ?2 and ?3)", nativeQuery = true)
List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);
another query
#Query(value = "select * from faculty_subject where subject_id in(:#{#subjectTop} and :#{#subjectMid} and :#{#subjectBottom})", nativeQuery = true)
List<FacultySubject> getFacultyBySubjects(#Param("subjectTop") long subjectTop,
#Param("subjectMid") long subjectMid,
#Param("subjectBottom") long subjectBottom);
Entity
#Entity
public class FacultySubject {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private long facultyId;
private int subjectId;
public FacultySubject(long facultyId, int subjectId) {
this.facultyId = facultyId;
this.subjectId = subjectId;
}
public FacultySubject() {
}
... getters and setters
#Query(value = "select * from faculty_subject where subject_id in(?1 , ?2 , ?3) group by faculty_id having count(*)==3", nativeQuery = true)
List<FacultySubject> getFacultyBySubjects(long subjectTop, long subjectMid, long subjectBottom);

SQL: Update does not affect any row

I want to update a dataset in a DB2/AS400 table.
The problem is if I there is string parameter in the parameters list the command does not find a row to update.
For example: If I run the command only with the company number the command will succeed. If I run the command with the company number and facility number the command fails.
Does anyone have any idea?
IDbConnection cn = Tools.GetCnApp();
try
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("UPDATE " + Tools.GetSchemeApp() + "/ChangeReasonAssignments");
sql.AppendLine(" SET Confirmed = #CONF, Confirmed_By = #CONFBY, Confirmed_At = #CONFAT");
sql.AppendLine(" WHERE Company = #CONO AND Facility = #FACI AND Department = #DEPT");
sql.AppendLine(" AND Production_Group = #PRGR AND Manufacturing_Order = #ORDR AND Order_Operation = #OPER");
sql.AppendLine(" AND Confirmed = 0");
IDbCommand cmd = cn.CreateCommand();
cmd.SetParameter("#CONO", this.CompanyNumber);
cmd.SetParameter("#FACI", this.FacilityNumber);
cmd.SetParameter("#DEPT", this.ProductionGroup.Department.Name);
cmd.SetParameter("#PRGR", this.ProductionGroup.Name);
cmd.SetParameter("#ORDR", this.ManufacturingNumber);
cmd.SetParameter("#OPER", this.OperationNumber);
cmd.SetParameter("#CONFBY", Base.User);
cmd.SetParameter("#CONFAT", DateTime.Now.ToString());
cmd.SetParameter("#CONF", 1);
cmd.CommandText = sql.ToString();
if (cmd.ExecuteNonQuery() > 0)
{
}
EDIT
The datatypes in database are:
Company: INTEGER
Facility: VARCHAR
Dpartment: VARCHAR
Production_Group: VARCHAR
Manufacturing_Order:INTEGER
Order_Operation: INTEGER
The datatypes in .NET are:
CompanyNumber: int
FacilityNumber: String
Departmentname: String
ProductionGroup: String
Manufacturingorder: int
OrderOperation: int
sql.ToString() results:
UPDATE TSAEDBDEV/ChangeReasonAssignments SET Confirmed = #CONF, Confirmed_By = #CONFBY, Confirmed_At = #CONFAT WHERE Company = #CONO AND Facility = #FACI AND Confirmed = 0
Try to set the string values into ': cmd.SetParameter("#DEPT", "'" + this.ProductionGroup.Department.Name + "'");

LINQ - querying top 5 with rank number

How do I return a top 5 with rank number using linq?
Dim Top5 = From A In DAO.Cache.Select(Of VO.Empresa).Take(5) Select A.Nome
I would like this result:
Rank Name
1 "Example Name"
2 "Example Name"
3 "Example Name"
4 "Example Name"
5 "Example Name"
You need to use the Select overload which provides the index:
Dim Top5 = DAO.Cache.Take(5).Select(Function(A, Index) New With { .Rank = Index, .Name = A.Nome })
(I kept the property spelling .Nome - though I suspect it may need to be .Name)
If grdDetail.RowCount < 10 Then
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = "00" & grdDetail.RowCount
ElseIf grdDetail.RowCount > 10 And grdDetail.RowCount < 100 Then
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = "0" & grdDetail.RowCount
Else
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = grdDetail.RowCount
End If
I'm not entirely sure I understand your question entirely: but I'm assuming you want to order your list to produce the top 5 in Rank - ascending order?
You can quite easily do this with the built in LINQ ordering syntax:
VB:
Dim Top5 = From o in objects Order By o.Rank Ascending Select o
C#: var top5 = from o in objects orderby o.Rank ascending select o
(surprising similar in this case /giggle)
For example, you could do the following:
C#:
void Main()
{
List<MyObject> objs = new List<MyObject>();
objs.Add(new MyObject{ Rank = 1, Message = "NUMBER ONE"});
objs.Add(new MyObject{ Rank = 3, Message = "NUMBER THREE"});
objs.Add(new MyObject{ Rank = 5, Message = "NUMBER FIVE"});
objs.Add(new MyObject{ Rank = 4, Message = "NUMBER FOUR"});
objs.Add(new MyObject{ Rank = 2, Message = "NUMBER TWO"});
var sortedobjs = from o in objs
orderby o.Rank ascending
select o;
Console.WriteLine(sortedobjs.ToList());
}
public class MyObject
{
public int Rank {get; set;}
public string Message {get; set;}
}
Which would spit out:
Rank Message
1 NUMBER ONE
2 NUMBER TWO
3 NUMBER THREE
4 NUMBER FOUR
5 NUMBER FIVE
HTH.