简体   繁体   English

如果所有子记录中的字段都相同,如何选择字段值

[英]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 物品

  • item_id name item_id名称
  • active_status active_status
  • etc 等等

Tasks 任务

  • task_id task_id
  • item_id item_id
  • active_status active_status
  • progress_status progress_status
  • etc 等等

The item's status is made up of the statuses of all of its tasks. 该项目的状态由其所有任务的状态组成。 Here's the logic... 这是逻辑……

  • If Item Status is Canceled or On Hold...return Item Active Status 如果项目状态被取消或处于保留中...返回项目活动状态
  • If there are no tasks, return Completed 如果没有任务,则返回已完成
  • If All Tasks are Active and NOT Superseded, then 如果所有任务都处于活动状态并且未被取代,则
  • ...return Not Started if all tasks are Not Started ...如果未启动所有任务,则返回未启动
  • ...return Completed if all tasks are Completed ...如果所有任务均已完成,则返回已完成
  • ...return On Hold if all tasks are On Hold ...如果所有任务都处于保留状态,则返回保留状态
  • Otherwise return Started 否则返回Started

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.

相关问题 选择特定字段不为NULL的所有记录 - Select all records whose specific field is not NULL 如何编写solr查询以检索数字字段值小于指定的所有记录? - How to write a solr query for retrieving all records with numeric field value less then specified? 如何为字段值选择“颜色”并滚动字段的参数值? - How to select a “Colour” for a field value and scroll parameter value of a field? 如何从两个表中选择两个在同一个字段中具有相同值的行? - How to select rows from two tables where both have the same value in the same field? 如何在单个sql表中合并两个记录的相同字段 - How to merge same field for two records in single sql table 如何更新与同一表的其他记录匹配的表字段 - How to Update a table field matching with another records of same table 如何在beanio的同一值上使用多个@Field? - How to use multiple @Field on the same value with beanio? 如何正确更改父子 object 中存在的字段的值 - How to properly change value of a field that exist in parent and child object 获取选择字段的当前值 - Get Current Value of Select Field 如果所有Xpath都相同,如何识别Selenium中的每个编辑文本字段 - How to identify each edit text field in Selenium if all their Xpath are same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM