Retrieve the list of data from database using hibernate criteria - spring

I have a table called employee_comp_field, where salary fields are available
comp_field id | year_id | compensation_field
1 101 salary
2 101 bonus
3 101 pf
4 101 allowance
5 102 salary
6 102 bonus
7 102 pf
8 102 allowance
Then I have another table where employee salary data get stored emp_compensation against each field. As you can see emp_id 10 has three set of records as he got three time salary hike in the same year(year_id=101), which can be identified by salary_order field.
id | year_id | emp_id | comp_field_id | amount | comp_order
1 101 10 1 10000 1
2 101 10 2 1000 1
3 101 10 3 1000 1
4 101 10 4 100 1
5 101 10 1 12000 2
6 101 10 2 100 2
7 101 10 3 10000 2
8 101 10 4 10000 2
9 101 10 1 15000 3
10 101 10 2 500 3
11 101 10 3 150 3
12 101 10 4 1500 3
13 101 11 1 13000 1
14 101 11 2 1300 1
15 101 11 3 null 1
16 101 11 4 150 1
I want to identify all the employees list with max salary_order
my desire output will be below:
id | year_id | emp_id | comp_field_id | amount | comp_order
9 101 10 1 15000 3
10 101 10 2 500 3
11 101 10 3 150 3
12 101 10 4 1500 3
13 101 11 1 13000 1
14 101 11 2 1300 1
15 101 11 3 null 1
16 101 11 4 150 1
as emp_id 10 got three time salary hike...so I retrieve the list of records with salary_order 3
and emp_id 11 got one ony so I retrieve that set of records ony with salary_order 1
Can someone please help me here, how to retrieve my desire output using hibernate criteria.
My thought is to first retrieve all the list based on emp_id and then using java stream if we can filter it out to get the desired output.
Please suggest the best possible way.

The best possible way.
is subjective. It can be the fastest, it can be the shortest. It could be anything.
I will give you an example of how you could build a query in mysql to replicate your output. This might be tricky to solve with Criteria though since the table is being self joined.
select a.*
from emp_compensation a
left outer join emp_compensation b on a.emp_id = b.emp_id
and a.comp_field_id = b.comp_field_id
and a.comp_order < b.comp_order
where b.emp_id is null

Related

Amazon QuickSight - Running Difference

I have the following table and want to add a column with running difference.
time_gap
amounts
0
150
0.5
19
1.5
2
6
1
7
4
my desired out is
time_gap
amounts
diff
0
150
150
0.5
19
131
1.5
2
129
6
10
119
7
4
115
What I've tried:
I duplicate the amounts column and used table calculation, difference, but got the difference between two consecutive rows instead:
time_gap
amounts
diff
0
150
0.5
19
-131
1.5
2
-17
6
10
8
7
4
-6
I tried some calculated fields formulas but that didn't work either.
thank you!

pl sql how can i fetch records with max values from another column by each room number

table
room-number
entry-number
electricity
n100
5
100
n100
4
90
n200
2
75
n200
1
69
n300
6
150
n300
5
111
result should be
room-number
electricity
n100
100
n200
75
n300
150
I'm not sure because I haven't tried it but
try this.
SELECT room-number, MAX(electricity) FROM table group by room-number

How to find max level in each path in a Oracle hierarchical SQL?

I would like to know the way to find the max level number in Oracle hierarchical SQL within the given path.
For example : If connect by clause starts with root 1 having below relation .
parent_id node_id votes
NULL 1 -
1 2 10
2 3 12
3 4 11
1 20 5
20 30 20
20 40 4
40 50 22
Here first 3 records belongs to one path with max levle 3.
Next 2 records belong to another path with max level 2.
Last two record belongs to another path with max level 3.
I need output with these max level within the given distinct path and minimum votes:
parent_id node_id LEVEL MAX_LEVL MIN_VOTE
1 2 1 3 10
2 3 2 3 10
3 4 3 3 10
1 20 1 2 5
20 30 2 2 5
1 20 1 3 4
20 40 2 3 4
40 50 3 3 4
1
|
--------------
| |
2 20
| |
3 --------------
| | |
4 30 40
|
50
Thanks,
Guru

count records by specifying many fields and many count clauses on sql oracle

I have this query:
SELECT * FROM PATIENT_MED_SPEC
which gives this result:
NUM_MALADE| NUM_MED| SPECIALITE
----------+--------+------------
3 53 Traumatologue
3 85 Anesthésiste
3 126 Radiologue
6 34 Pneumologue
6 85 Anesthésiste
6 114 Traumatologue
6 135 Anesthésiste
13 4 Orthopédiste
13 8 Cardiologue
13 114 Traumatologue
21 19 Traumatologue
21 64 Radiologue
21 135 Anesthésiste
23 4 Orthopédiste
23 8 Cardiologue
23 88 Cardiologue
And I want to get the result below using count and group by clauses
NUM_MALADE| count_MED| count_SPEc
----------+----------+------------
3 3 3
6 4 3
13 3 3
21 3 3
23 3 2
i.e for every MALADE (patient) we count the MEDECIN's
(doctors) number and the number of SPECIALITES concerned
You could use COUNT DISTINCT:
SELECT MALADE, COUNT(*) AS count_MED, COUNT(DISTINCT SPECIALITE) AS num_SPEC
FROM PATIENT_MED_SPEC
GROUP BY MALADE

Oracle 10g ROLLUP and TO_CHAR function

I have 2 queries:
select zam_klt_id,zam_order_date, count(*) as sum from orders
group by rollup (zam_order_date,zam_klt_id);
which produce output:
ZAM_KLT_ID ZAM_order_date SUM
---------- ------------------- ----------
1002 98/03/13 1
98/03/13 1
1004 98/03/14 1
98/03/14 1
1003 98/09/11 1
98/09/11 1
1003 99/01/05 1
99/01/05 1
1003 99/03/01 1
99/03/01 1
1003 99/07/26 1
99/07/26 1
1003 99/10/30 1
99/10/30 1
1002 00/05/08 1
00/05/08 1
1004 00/06/14 1
00/06/14 1
00/07/12 1
00/07/12 1
1000 00/12/10 2
00/12/10 2
1004 00/12/21 1
00/12/21 1
13
This is OK, under every date there is short summary (count) like in
1000 00/12/10 2
00/12/10 2
However then I wanted to know in every year how many clients made orders, so I changed preveious query (zam_order_date was changed to to_char(zam_order_date,'yyyy'))
select zam_klt_id,to_char(zam_order_date,'yyyy'), count(*) as sum from orders
group by rollup ((to_char(zam_order_date,'yyyy'),zam_klt_id));
will produce output
ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY') SUM
---------- ----------------------------------- ----------
1002 1998 1
1003 1998 1
1004 1998 1
1003 1999 4
2000 1
1000 2000 2
1002 2000 1
1004 2000 2
13
9 rows selected
this time there is no summary under every date (year in this case), I think the output should look like this :
ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY') SUM
---------- ----------------------------------- ----------
1002 1998 1
1003 1998 1
1004 1998 1
*1998* *3*
etc
Why summaries are not added this time, is it have something to do with the to_char function?

Resources