let´s suppose I have a table with three columns (ID,Description, Sequence) whatever you prefer, and I'd like to order by this string sequence:
ID | Description | Sequence
1 | 'Test1' | '1.8'
2 | 'Test2' | '1.9'
3 | 'Test3' | '1.10'
4 | 'Test4' | '1.3'
According to this post (Ordering dot-delimited numeric sequences (e.g., version numbers)), I can use STING_TO_ARRAY('.') as int[] and it works just fine on the DB.
I've tried the following, but I get unexpected token: error:
(" order by STRING_TO_ARRAY(c.sequence, '.')::Integer[] ");
Is it possible to use it in a HQL query?
Related
I have a project created by java code, and a database that has two tables (table1 and table2)
looks like this:
-------table1--------
Name | Ref
__________|__________
A | 100
B | 200
__________|__________
and
-------table2--------
Name | Q
__________|__________
A | 12
B | 10
A | 14
__________|__________
I try create a SQL query to get this result:
Name | Ref | SUM(Q)
__________|_________|__________
A | 100 | 26
B | 200 | 10
__________|_________|__________
I wrote this query
query ="SELECT table1.Name,table1.Ref FROM table1 WHERE table1.Name=(SELECT table2.Name,SUM(table2.Q) FROM table2 GROUP BY table2.Name)";
but my code not working and I'm getting this error:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::5.0.0 row column count mismatch
any suggestions to correct my query code?
Use this:
SELECT table1.Name,table1.Ref
FROM table1
WHERE table1.Name IN (SELECT table2.Name FROM table2)
Your subquery has two problems.
It returns more than one column, so it's not suitable for matching with = or IN.
It may return more than one row, so it's not suitable for matching with =.
So let's say I have the following table for a student:
+----+------------+-----------+---------------------+----------+------------+
| first_name | last_name | ID | Birthday | Grade | Periods |
+----+------------+-----------+---------------------+----------+------------+
| John | Doe | 123456 | 1/1/2004 | 11 | 7 |
+----+------------+-----------+---------------------+----------+------------+
How could I get the ResultSet for just that one student by just using their ID for example?
Could the query be SELECT ID(123456) FROM Students" or something along those lines? I found some answers online as to how I could loop through all the Students for example and print out that data or do something else with it, but not how to get the data for a singular element in the table based on one attribute.
In short, I just want to be able to get all the data on a student from just their ID number.
I am working on a project Spring and Java, generated using JHipster. I want to filter on table that doesn't have a direct relationship with another.
My purpose is almost asked in a previous similar question
Write Spring Specification with multiple inner join & other conditions
But in my case , I ve two unrelated entities :
Consultant (id : Long , FullName : string , profileRank : Enum of string )
Rank (id : Long , level : Enum of string , rate : Double )
Consultant | Rank
|
id | FullName | profileRank | id | level | rate
1 | aaaaa | 'ONE' | 1 | 'ONE' | 1
2 | bbbbbb | 'THREE' | 2 | 'TWO' | 2
3 | cccccc | 'FOUR' | 3 | 'THREE' | 3
4 | dddddd | 'THREE' | 4 | 'FOUR' | 4
I want to filter consultant list by rate using level
Example : get consultants with rate greater than 3
Expected result
id | FullName | profileRank
3 | cccccc | 'FOUR'
I ve searched in documentation and many articles without get it to work please how to achieve that .
You don't need to write a specification for your case.
Fetch all the ranks with levels and rates
Filter these values and keep only the ones greater than 3 (step 1 and 2 can be combined). The result will be a List<Rank> that contains only FOUR rank
list.stream(rank => rank.level).collect(toList())
The result of step 3 will be passed to a repository method like List<Consultant> findByProfileRankIn(List<String> levelNames)
Another alternative would be joins something like this Joining tables without relation using JPA criteria
If you still want a spec that's also possible. Spring Data Join with Specifications
I am using Microsoft SQL Server with already stored data.
In one of my tables I can find data like:
+--------+------------+
| id | value |
+--------+------------+
| 1 | 12-34 |
| 2 | 5678 |
| 3 | 1-23-4 |
+--------+------------+
I realized that the VALUE column was not properly formatted when inserted.
What I am trying to achieve is to get id by given value:
SELECT d.id FROM data d WHERE d.value = '1234';
Is there any way to format data in column just before SELECT clause?
Should I create new view and modify column in that view or maybe use complicated REGEX to get only digits (with LIKE comparator)?
P.S. I manage database in Jakarta EE project using Hibernate.
P.S.2. I am not able to modify stored data.
One method is to use replace() before the comparison:
WHERE REPLACE(d.value, '-', '') = '1234'
I have two tables which are related:
+-----------+ +----------+
| First | * 1 | Second |
+-----------+ --------- +----------+
| fk_Second | | id |
| version | | ... |
| ... | | y |
| x | +----------+
+-----------+
Hibernate has a ManyToOne definition from First to Second. The {fk_Second, version} is a composite-id of the First table (although I don't think it's relevant in this case).
I am trying to write Criteria call, which in SQL would look like as:
SELECT * FROM First WHERE
First.fk_Second = Second.id AND
First.x = Second.y
I'm finding trouble in generating the last bit - the extra join condition.
Criteria c = session.createCriteria(First.class);
.createCriteria("second", "join_between_first_and_second")
.add(Restrictions.eqProperty("y", "x") // <- fails: "x" is not found
I can not use HQL queries in this situation. Is there any way writing this differently? Can this be written avoiding subquery/DetachedCriteria?
Criteria c = session.createCriteria(First.class, "first");
c.createAlias("first.second", "theSecond");
c.add(Restrictions.eqProperty("first.x", "theSecond.y");
If you don't prepend an alias to your property, the property is considered part of the root entity of the criteria (First in this case).
Try HQL 'With' clause..
SELECT f.* FROM First f left join Second s ON f.fk_Second = s.id with f.x = s.y;