简体   繁体   English

表中有 2 个相同 ID 的元组,并且这两行的其他一些列的值不同

[英]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 = 111case_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.

相关问题 如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行 - How to select rows between two different columns with two different input values on same table on mysql 将两个查询合并到同一个表中的一个(相同列/不同行) - Combine two queries into one in same table (same columns/different rows) 连接具有相同ID和相同列名但值不同的两个表 - joining two tables with same id and same name of columns but different values 如何在 mysql 中的某些列中组合具有相同 ID 但不同值的行 - How to combine rows with same ID but different values in certain columns in mysql SQL 合并两行ID相同但列值不同的行 - SQL Merge two rows with same ID but different column values 如何将某些行的列值设置为与同一表中的其他行相同? - How to set the column values of some rows the same as the some other ones in the same table? 同一张表的两个不同列的MySql Distinct值 - MySql Distinct values of two different columns of the same table 从同一表的两个不同列中选择DISTINCT值 - SELECTING DISTINCT values from two different COLUMNS of the same table 选择具有相同id但不同值的不同行 - Selecting different rows with same id but different values 将表中的行复制到具有不同ID的同一表中 - copying rows in a table into the same table with a different id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM