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.
There are 3 tables, table 1 contains 4 rows(categories) of financial data(fixed income, equities, ratings, listings) with their systemId.
Table 2 contains over 1000 rows of financial data which may fall under any of those 4 categories. Table 2 also has a row called STATUS which may contain a value of: "running","fail", or "standby".
Table 3 holds general information about the data sets and has both the value of id and 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).
TABLE 1
category systemId ???STATE???
fixed income 1 ?
listings 2 ?
ratings 3 ?
equities 4 ?
TABLE 2
ID STATUS
45421158 failed
2121158 running
9464548888 running
454548888 standby
9948158 running
78748158 running
TABLE3
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.
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
.
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()
:
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:
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
. If this is not the case change to inner
joins
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.