[英]How to select the field value if field in all child records are the same
I don't know if I'm wording this question correctly, but here it goes. 我不知道我是否正确地表达了这个问题,但是到了。
This is a web application using java, oracle, hibernate. 这是一个使用java,oracle,hibernate的Web应用程序。
I have 2 tables in a one (items) to many (tasks) relationship. 我在一个(项目)对许多(任务)的关系中有2个表。
Items 物品
Tasks 任务
The item's status is made up of the statuses of all of its tasks. 该项目的状态由其所有任务的状态组成。 Here's the logic... 这是逻辑……
I want to do this using SQL and map it to a field in my hibernate mapping file. 我想使用SQL并将其映射到休眠映射文件中的字段中。
I've tried many things over the past several days, and can't seem to get it to work. 在过去的几天里,我已经尝试了很多事情,但似乎无法使其正常工作。 I tried grouping the records and if 1 record was found, return that status. 我尝试对记录进行分组,如果找到1条记录,则返回该状态。 I've used decode, case, etc. 我用过解码,大小写等
Here are a few examples of things I've tried. 这是我尝试过的一些例子。 In the second example I get a 'not a single group group function' error. 在第二个示例中,我收到“不是单个组组函数”错误。
Any thoughts? 有什么想法吗?
select decode(i.active_status_id, 'OH', i.active_status_id, 'Ca', i.active_status_id,t.progress_status_id)
from tasks t
LEFT OUTER JOIN Items i
ON i.item_id = t.item_id
where t.item_id = 10927815 and t.active_status_id = 'Ac' and t.active_status_id != 'Su'
group by i.active_status_id, t.progress_status_id;
select case
when (count(*) = 1) then progress_status_id
else 'St'
end
from
(select progress_status_id
from tasks t
where t.item_id = 10927815 and (t.active_status_id = 'Ac' and t.active_status_id != 'Su') group by t.progress_status_id)
perhaps somthing like this 也许是这样的
SELECT
item_id
, CASE
WHEN active_status IN ('Canceled', 'On Hold') THEN active_status
WHEN t_num = 0 THEN 'Completed'
WHEN flag_all_active = 1 AND flag_all_not_started = 1 THEN 'Not Started'
WHEN flag_all_active = 1 AND flag_all_completed = 1 THEN 'Completed'
WHEN flag_all_active = 1 AND flag_all_on_hold = 1 THEN 'On Hold'
ELSE 'Started'
END
FROM
(
SELECT
i.item_id
, i.active_status
, sum(CASE WHEN t.task_id is NULL THEN 0 ELSE 1 end ) as t_num
, MIN( CASE t.active_status WHEN 'Ac' THEN 1 ELSE 0 END ) as flag_all_active
, MIN( CASE t.progress_status_id WHEN 'Not Starten' THEN 1 ELSE 0 END ) as flag_all_not_started
, MIN( CASE t.progress_status_id WHEN 'Completed' THEN 1 ELSE 0 END ) as flag_all_completed
, MIN( CASE t.progress_status_id WHEN 'On Hold' THEN 1 ELSE 0 END ) as flag_all_on_hold
FROM
items i
left outer join tasks t on (t.item_id = i.item_id )
group by i.item_id
)
;
If you're using annotations you can use @Formula("sql query here")
for your derived properties. 如果使用批注,则可以将@Formula("sql query here")
用于派生属性。 See Hibernate formula docs for a (surprisingly brief) explanation. 请参阅Hibernate公式文档以获取(令人惊讶的简短)解释。
Alternatively, since you're dealing with relatively large lists of items, it would be better to make the status calculations part of your initial query thus avoiding the database getting hammered by hundreds or thousands of requests. 另外,由于您要处理的项目列表相对较大,因此最好将状态计算作为初始查询的一部分,从而避免数据库受到成百上千个请求的影响。 This is what will probably happen if you iterate over each item in the list. 如果您遍历列表中的每个项目,可能会发生这种情况。
I would recommend joining the status calculation to whatever query you are using to generate your list (presumably in a NamedQuery). 我建议将状态计算加入到用于生成列表的任何查询中(大概在NamedQuery中)。 This lets your database do all the heavy lifting without being slowed down by the network, which is what it is best at. 这样一来,您的数据库就可以完成所有繁重的工作,而不会因为网络速度而减慢,这是最好的选择。 The Hibernate docs give lots of helpful examples of queries you can try. Hibernate文档提供了许多有用的查询示例,您可以尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.