简体   繁体   English

如果另一个表的某个列中的任何值包含相同的“字符串”,则需要添加包含“string”的列

[英]Need to add column which contains “string” if any of the values in a certain column from another table contain that same “string”

I'm trying to modify a query to add a column whos value must be "fail" if any of the values with the same id from another table has "fail" in a certain column. 我正在尝试修改查询以添加一个列,如果某个列中具有相同id的任何值在某个列中“失败”,则该值必须为“fail”。

There are 3 tables, table 1 contains 4 rows(categories) of financial data(fixed income, equities, ratings, listings) with their systemId. 有3个表,表1包含4行(类别)的财务数据(固定收益,股票,评级,列表)及其systemId。

Table 2 contains over 1000 rows of financial data which may fall under any of those 4 categories. 表2包含超过1000行的财务数据,这些数据可能属于这四类中的任何一类。 Table 2 also has a row called STATUS which may contain a value of: "running","fail", or "standby". 表2还有一行称为STATUS ,其中可能包含以下值:“running”,“fail”或“standby”。

Table 3 holds general information about the data sets and has both the value of id and systemId . 表3包含有关数据集的一般信息,并且具有idsystemId的值。

I want to add a column to TABLE1(categories) called STATE which contains "fail" or "standy" if any of the rows with the the same systemId contains the value "fail" or "standby" ,and only show "running" if all rows with the same systemId (table2) have "running" in their status column on (table2). 我想在TABLE1(类别)中添加一个名为STATE的列,其中包含“fail”或“standy”,如果具有相同systemId的任何行包含值“fail”或“standby”,并且仅显示“running”,如果具有相同systemId (table2)的所有行在其(table2)的状态列中具有“running”。


TABLE 1 表格1

         category             systemId      ???STATE??? 

         fixed income         1                 ?
         listings             2                 ?
         ratings              3                 ?
         equities             4                 ?

TABLE 2 表2

  ID                    STATUS

  45421158              failed 
  2121158              running  
  9464548888             running
  454548888             standby
  9948158              running
  78748158              running

TABLE3 表3

  id                    **systemId**
  45421158              1
  98721158              1
  454548888             2
  6888888             2
  78748158              3
  9978158              3

I have tried so many different joins and sub queries and have had no luck. 我尝试了很多不同的连接和子查询,但没有运气。 I am a UI dev and dont have access to the database only a sample which runs on java through the cache so I cant add new tables or anyting like that, it has to be 1 query so I can modiy the resource. 我是一个UI开发人员,没有访问数据库只有一个通过缓存在java上运行的示例,所以我不能添加新表或任何类似的,它必须是1查询所以我可以修改资源。

Thanks in advance you will be legit saving ma life! 在此先感谢您将合法拯救生命!

I can't quite tell what the columns in your table are. 我不清楚你的表中的列是什么。 But for the logic that you want, one method uses CASE and EXISTS . 但是对于你想要的逻辑,一种方法使用CASEEXISTS

The query looks like this, although I am not sure what the correlation conditions are exactly (that is, what matches the rows in the two tables): 查询看起来像这样,虽然我不确定相关条件是什么(即,匹配两个表中的行的内容):

select c.*,
       (case when exists (select 1
                          from servicemetric.servicemetric m
                          where m.category = c.category and
                                m.status = 'fail'
             then 'fail'
             when exists (select 1
                          from servicemetric.servicemetric m
                          where m.category = c.category and
                                m.status = 'standby'
             then 'standby'
             else 'running'
        end) as imputed_status
from node.categories c;

You can also implement the logic using conditional aggregation: 您还可以使用条件聚合实现逻辑:

select m.category,
       (case when sum(case when m.status = 'fail' then 1 else 0 end) > 0 then 'fail'
             when sum(case when m.status = 'standby' then 1 else 0 end) > 0 then 'standby'
             else 'running'
        end) as status
from servicemetric.servicemetric m
group by m.category;

Or even using coalesce() : 甚至使用coalesce()

select m.category,
       coalesce(max(case when m.status = 'fail' then status end),
                max(case when m.status = 'standby' then status end),
                max(m.status)
               )
from servicemetric.servicemetric m
group by m.category;

You must join the 3 tables and group by category and systemid: 您必须group by类别和systemid加入3个表和group by

select
  t1.category,
  t1.systemid,
  case
    when sum(case when t2.status = 'failed' then 1 else 0 end) > 0 then 'failed'
    when sum(case when t2.status = 'standby' then 1 else 0 end) > 0 then 'standby'
    when sum(case when t2.status = 'running' then 1 else 0 end) > 0 then 'running'
  end status
from table1 t1
left join table3 t3 on t3.systemid = t1.systemid
left join table2 t2 on t2.id = t3.id
group by t1.category, t1.systemid

I used left joins in case there are system ids missing from table3 . 我使用left连接以防table3缺少系统ID。 If this is not the case change to inner joins 如果不是这种情况则更改为inner连接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用另一个表中同一列的值替换列中的字符串值 - Replace a string value in a column by values from same column in another table 替换包含表中列的任何结果的字符串的一部分 - Replace part of of a string that contains any results from a column in a table 如何检查/选择字符串是否包含表列值中的值? - How to check/select if a string contains value from a table column values? Bigquery SELECT * FROM表,其中column ='string'不返回任何值 - Bigquery SELECT * FROM table where column = 'string' not returning any values 需要查询以获得一个表的列值,而该值不在另一表的字符串列中 - Query needed to get an column values of one table which are not there in string column of another table 在列表中添加值,该列表是蜂巢中的字符串列 - Add values in a lists which is a string column in hive 如何根据字符串聚合列从另一个表中添加项目 - How to add items from another table based on a string aggregated column 从表中的列中删除字符串的某些部分 - removing certain part of string from column in table 如何将数组列添加到包含同一表中另一列的值的表中 - How to add an array column to a table containing values from another column in the same table 我可以将列的默认值设置为同一表中同一行上另一列的字符串吗? - Can I set a column's default to be a character string from another column on the same row within the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM