I need to get record like :-
toolTypeId, quantity, toolName , onTruck, Damage, loss
2 50 test1 20 25 5
onTruck, Damage, loss :- total records from list
first 3 are from toolsType table and last 3 from userTools.
My model classes :-
ToolsType.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String toolName;
private Long quantity;
UserTools.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
private ToolsType toolsType;
private Long loss;
private Long onTruck;
private Long damage;
I am trying something like that in my Repository file :- select count(toolstype.id), toolstype.toolName, quantity FROM toolstype GROUP BY(toolstype.id);
I know its not correct Please help me for correct query for this issue.
Thanks
Use this query. Hope it will help you. You need to use alias
in this case.
select count(ttype.id), ttype.toolName, ttype.quantity FROM toolstype as ttype GROUP BY(ttype.id);
UPDATED Query is given below:
select count(ttype.id), ttype.toolName, ttype.quantity, utools.onTruck, utools.damage, utools.loss
FROM toolstype ttype, usertools utools
where ttype.id = utools.id
GROUP BY ttype.toolName, ttype.quantity, utools.onTruck, utools.damage, utools.loss
You must use this JPQL query:
select tt.id, count(tt.id), tt.toolname, ut.onTruck, ut.damage, ut.loss
from UserTools ut
join ut.toolsType tt
group by tt.id, tt.toolname, ut.onTruck, ut.damage, ut.loss
You need to put all select
values in the group by
clause for the query to group the results.
UPDATE:
If you need the sum of UserTools
values and all the ToolsType
values in the result, the query would be:
select tt.id, count(tt.id), tt.toolname, sum(ut.onTruck), sum(ut.damage), sum(ut.loss)
from ToolsType tt, UserTools ut
where tt.id = ut.toolsType.id
group by tt.id, tt.toolname, ut.onTruck, ut.damage, ut.loss
I also think that the relation between UserTools
and ToolType
would be @ManyToOne
instead of @OneToOne
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.