[英]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包含有关数据集的一般信息,并且具有id和systemId的值。
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
. 但是对于你想要的逻辑,一种方法使用
CASE
和EXISTS
。
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.