[英]There are 2 tuples for same ID in an table and the values of some other columns for those two rows are different
I have used the below query :我使用了以下查询:
select distinct e.case_id,e.case_name,s.script_name,s.script_type,e.Result
from executions_table e
left outer join scripts_table s on s.Case_ID=e.case_id
where e.case_id like '11%'
The result obtained is :得到的结果是:
How can I choose the row which has the script_type as automation over manual for case_id = 111 and for case_id = 113 as there are 2 instances and the result is passed and also choose the rows 112 where the script_type is manual and passes where there is only manual.如何为case_id = 111和case_id = 113选择具有 script_type 作为自动化而不是手动的行,因为有 2 个实例并且结果被传递,并且还选择了 script_type 为手动的行 112 并通过只有手动的。
Thanks for your help in advance.提前感谢您的帮助。
First of all, the picture you took is not visible.首先,你拍的照片是不可见的。 But I will try to help as best as I can.
但我会尽我所能提供帮助。 The query you typed says
where e.case_id like '11% '
.您输入的查询说明
where e.case_id like '11% '
。 This is not healthy.这不健康。 Because there may be many IDs starting with 11. I can't give a clear answer because there is no photograph of the error.
因为可能有很多以11开头的ID。我无法给出明确的答案,因为没有错误的照片。 But if you just want to call a query for 111 and 113, you can try
IN ()
.但是如果你只是想调用 111 和 113 的查询,你可以试试
IN ()
。 If it's still unsolved, you can ask for help again.如果还是没有解决,可以再次寻求帮助。
You could filter unwanted records with a not exists
condition:您可以使用
not exists
条件过滤不需要的记录:
select distinct e.case_id, e.case_name, s.script_name, s.script_type, e.Result
from executions_table e
left outer join scripts_table s on s.case_id = e.case_id
where
e.case_id like '11%'
and (
s.script_type = 'Automation'
or not exists (
select 1
from scripts_table s1
where
s1.case_id = s.case_id
and s1.script_name <> s.script_name
and s1.script_type = 'Automation'
)
)
If I understand correct that you wants only 3 row to return from your query and If you are using MySQL version 8, you can use CTE and Row_Number to achieve your requirement-如果我理解正确,您只希望从查询中返回 3 行,并且如果您使用的是 MySQL 版本 8,则可以使用 CTE 和 Row_Number 来实现您的要求-
WITH CTE AS(
select distinct e.case_id,e.case_name,s.script_name,s.script_type,e.Result
from executions_table e
left outer join scripts_table s on s.Case_ID=e.case_id
where e.case_id like '11%'
),
CTE2 AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY case_id ORDER BY script_type) RN
FROM CTE
)
SELECT * FROM CTE2 WHERE RN = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.