Hibernate criteria to fetch the records? - java

I have below records in table.
col1 col2 col3
------------------------
1 Abc IN
2 DEF CA
3 Xyz IN
4 Cae CA
5 Pty IN
6 Zwe DE
7 Zwf US
Here User sends an Input like IN or CA or DE etc. User input has to be mapped against col3. Now I need to query all the records from the table but the records matching the user input (IN or CA or DE) should appear first in the list then all other records should appear. How can I do it using hibernate criteria?
I need the results in below the order if user sends IN as an input.
1 Abc IN
5 Pty IN
3 Xyz IN
2 DEF CA
4 Cae CA
6 Zwe DE
7 Zwf US

You could try to use ORDER BY CASE construct:
order by case when <your entity>.col3 = :parameter then '0' else '1' end asc

There are two ways to solve this problem:
1. Create two queries one with equal to another with not equal to and all results for both in single list.
2. If you don't want to query database twice then you have to write algo in java that will remove elements for your input from list and add it another list and after iteration add remaining list at the end.

try the case ... when statements:
select *, ( case when col3=:input then '0' | col3 else col3 end) as sorter from table order by sorter asc
not sure, if it works, but if it does it would be exactly what you want

Related

Oracle order by via JPA with numeric first

I have column data like this in my db
data
-----
1
2
A
3
4
B
I have below nls settings in place
SELECT * From NLS_SESSION_PARAMETERS;
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_COMP BINARY
When I query the db for **select data from <mytable> order by data ** I am getting the result as below
data
-----
1
2
3
4
A
B
The same query via hibernate is giving the results with alphabets first and numeric later
data
-----
A
B
1
2
3
4
But, I want column to be displayed with numbers first and alphabets later via hibernate
data
------
1
2
3
4
A
B
Can someone help me with this.
You can use regularexpressions to sort in such a way,
select data from <mytable> order by REGEXP_REPLACE(data ,'[^0-9]'), REGEXP_REPLACE(data ,'[0-9]')
That depends on NLS_SORT parameter's value. For example, in my database it is set to CROATIAN and the result is what you wanted:
SQL> select * From nls_session_parameters where parameter = 'NLS_SORT';
PARAMETER VALUE
-------------------- --------------------
NLS_SORT CROATIAN
SQL> with test (data) as
2 (select '1' from dual union all
3 select '2' from dual union all
4 select 'A' from dual union all
5 select '3' from dual union all
6 select '4' from dual union all
7 select 'B' from dual
8 )
9 select data
10 from test
11 order by data;
D
-
A
B
1
2
3
4
6 rows selected.
However, in your database, NLS_SORT = BINARY. Let's try it:
SQL> alter session set nls_sort = 'BINARY';
Session altered.
SQL> with test (data) as
2 (select '1' from dual union all
3 select '2' from dual union all
4 select 'A' from dual union all
5 select '3' from dual union all
6 select '4' from dual union all
7 select 'B' from dual
8 )
9 select data
10 from test
11 order by data;
D
-
1
2
3
4
A
B
6 rows selected.
SQL>
Right; the wrong result.
Therefore, modify NLS_SORT, if that's an option. See valid values by
SQL> select * From v$nls_valid_values where parameter = 'SORT';
PARAMETER VALUE ISDEP
-------------------- -------------------- -----
SORT BINARY FALSE
SORT WEST_EUROPEAN FALSE
SORT XWEST_EUROPEAN FALSE
SORT GERMAN FALSE
<snip>
I believe one of the two options below should work,
Use NLSSORT function in the order by clause explicitly.
select * from my_table order by NLSSORT(data,'NLS_SORT=BINARY');
Create a function based index on the column,
create index mytable_nlssort_index on my_table(nlssort(data, 'nls_sort=''BINARY'''))
Use the query as below when the column is indexed.
select data from my_table order by data;

How to match number "start with" through query sqlite

Is there any way to match the query, for example, I want to search my number against the rules (table name) through query.
I want to match the number which starts with "333" rules .........
1)3322323
Here is my query
SELECT * FROM demo where rules like '33322323';
I want above query return true. because it matches with my rule.
Below is my table.
id | rules
...............
1 | 333
2 | 22
3 | 442
I have sample data 1) 33331235, 2) 2354545 3) 4424545454 4) 22343434
Case 1 (matching data 1) 33331235)
Suppose I want to check my 33331235 with my rules table so my sample data match with my
row 1 which is 333, because of my data start with 333..... it should return true because of it matched.
Case 2 (matching data 2) 2354545)
Suppose I want to check my 2354545 with my rules table so my sample data does not match with my
Any row because my no rule applies on it..... it should return true because of it matched.
Case 3 (matching data 1) 4424545454)
Suppose I want to check my 4424545454 with my rules table so my sample data match with my
row 1 which is 442, because of my data start with 442..... it should return true because of it matched.
Solved.
I solved this with the help of Forpas I used this query to match number start with
SELECT * FROM demo where '333434334 like rules ||'%';
The number at the end of the string when I use this query.
SELECT * FROM demo where '333434334' like '%' || rules;
The number anywhere in the string then I use this query.
SELECT * FROM demo where '333434334' like '%' || rules ||'%';
You need to use the operator LIKE.
You have tagged your question with both MySQL and SQLite.
For MySQL:
SELECT * FROM demo where '33322323' like concat(rules, '%');
For SQLite:
SELECT * FROM demo where '33322323' like rules || '%';
The above code will return all rows where the rules column value is the starting chars of 33322323.

FOREACH in cypher - neo4j

I am very new to CYPHER QUERY LANGUAGE AND i am working on relationships between nodes.
I have a CSV file of table containing multiple columns and 1000 rows.
Template of my table is :
cdrType ANUMBER BNUMBER DUARTION
2 123 456 10
2 890 456 5
2 123 666 2
2 123 709 7
2 345 789 20
I have used these commands to create nodes and property keys.
LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
CREATE (:ANUMBER {aNumber:ROW.aNumber} ),
CREATE (:BNUMBER {bNumber:ROW.bNumber} )
Now I need to create relation between all rows in the table and I think FOREACH loop is best in my case. I created this query but it gives me an error. Query is :
MATCH (a:ANUMBER),(b:BNUMBER)
FOREACH(i in RANGE(0, length(ANUMBER)) |
CREATE UNIQUE (ANUMBER[i])-[s:CALLED]->(BNUMBER[i]))
and the error is :
Invalid input '[': expected an identifier character, whitespace,
NodeLabel, a property map, ')' or a relationship pattern (line 3,
column 29 (offset: 100)) " CREATE UNIQUE
(a:ANUMBER[i])-[s:CALLED]->(b:BNUMBER[i]))"
I need relation for every row. like in my case. 123 - called -> 456 , 890 - called -> 456. So I need visual representation of this calling data that which number called which one. For this I need to create relation between all rows.
any one have idea how to solve this ?
What about :
LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
CREATE (a:ANUMBER {aNumber:ROW.aNumber} )
CREATE (b:BNUMBER {bNumber:ROW.bNumber} )
MERGE (a)-[:CALLED]->(b);
It's not more complex than that i.m.o.
Hope this helps !
Regards,
Tom

formatting query results using oracle

I want to format query results in oracle and save them into an output file.
I tried this query:
spool "result.txt"
SELECT STA_CODE,DATE_CREATION,DATE_FIN_INSTANCE,DATE_FIN_TRAITEMENT FROM DEMANDE;
spool off;
In my output file, the result looks like:
STA_CODE DATE_CRE DATE_FIN DATE_FIN
------------------------- -------- -------- --------
200 09/05/17 09/05/17 09/05/17
400 09/05/17 09/05/17 09/05/18
I want then to write a java code that takes for each line the result and match it with name of column: for example STA_CODE=200, STA_CODE=400, DATE_CRE=09/05/17, DATE_CRE=09/05/18 ....
I'm biginner in JAVA and I can't write that Bit of code. Is possible to directly format query results and then parse the output file without doing any transformation with java.
If you want each row separated, then use
SELECT 'STA_CODE='||STA_CODE
||', DATE_CRE=' ||to_date(DATE_CREATION,'DD/MM/YY')---other values
from DEMANDE
If you want all STA_CODE first and then all DATE_CRE and then other columns in one line, separated by comma, use something like
select listagg(col1,', ') within group (order by seq)
from (
SELECT 1 as seq,'STA_CODE='||STA_CODE as col1 from DEMANDE
union
SELECT 2 as seq,'DATE_CRE='||to_date(DATE_CREATION,'DD/MM/YY') from DEMANDE
union
---- other select queryies separated by union.
)
NOTE: You cannot guarantee order among each row. So it might happen that second STA_CODE come first and first DATE_CRE come second. To garuntee that, order by a column in all union queries.

PostgreSQL. Search for a value through a list of attributes with a specific id

I need to make search for an object by specific attributes. I have a table item_attribute:
item_attribute             item_attribute_type_fk            item_fk              value_text
1 1 1 bbbb
2 2 1 450-240-310
3 3 1 800x250
4 4 1 2,2
5 1 2 HBO
etc
I've tried this:
SELECT item,name,sale_price,producer,producer_code,store_price,I.type_name
FROM ((item INNER JOIN unit_type ON unit_type_fk=unit_type)
INNER JOIN item_store ON item=item_fk)AS I
INNER JOIN item_attribute AS A ON I.item=A.item_fk
WHERE store_price BETWEEN 200.0 AND 300.0
AND (UPPER(value_text) LIKE UPPER('%b%') AND item_attribute_type_fk=1)
AND (UPPER(value_text) LIKE UPPER('%2%') AND item_attribute_type_fk=4)
GROUP BY item,I.type_name ORDER BY item
Result should be row, where item_fk = 1, but in reality select returns zero rows. If I change AND to OR between attributes that will result in several rows, which is not exactly what I want, because search need to return only that object, that has all attributes, that user typed in in search field. How should I change code, that it will look for a value only through attributes with specific item_attribute_type_fk and return row with object, that has all that attributes?
You appear to be using an EAV-like schema.
To query multiple values in EAV you have to join the same table multiple times. e.g.
SELECT item,name,sale_price,producer,producer_code,store_price,I.type_name
FROM
item
INNER JOIN unit_type ON unit_type_fk=unit_type
INNER JOIN item_store ON item=item_fk)AS I
INNER JOIN item_attribute AS a1 ON I.item = a1.item_fk
INNER JOIN item_attribute AS a2 ON I.item = a2.item_fk -- note second join
WHERE store_price BETWEEN 200.0 AND 300.0
AND (UPPER(a1.value_text) LIKE UPPER('%b%') AND a1.item_attribute_type_fk=1)
AND (UPPER(a2.value_text) LIKE UPPER('%2%') AND a2.item_attribute_type_fk=4)
GROUP BY item, I.type_name
ORDER BY item
Consider transitioning to using json or hstore for your attributes. It's lots less painful to query.

Categories