SQL查询 - 消除过程

时间:2017-09-01 15:03:07

标签: sql

我有3个查询需要将单个输出组合成一个查询,但似乎无法获得我需要的最终结果。任何建议将不胜感激。

SampleID | TestID | Microcomments  |ProductID |ComapanyID
---------+--------+----------------+----------+----------
 111101  | Test1  | Satisfactory   | ABC123   | 1111
 111101  | Test2  | No Test        | ABC123   | 1111
 111101  | Test3  | Unsatisfactory | ABC123   | 1111
 222202  | Test2  | Satisfactory   | A21      | 2222
 222202  | Test3  | Unsatisfactory | A21      | 2222
 333301  | Test1  | Satisfactory   | AB11     | 3333
 333301  | Test3  | No Test        | AB11     | 3333
 444403  | Test2  | Unsatisfactory | ABCD123  | 4444
 444403  | Test3  | No Test        | ABCD123  | 4444
 555504  | Test1  | Unsatisfactory | BA123    | 5555
 555504  | Test2  | Unsatisfactory | BA123    | 5555
 666601  | Test3  | Satisfactory   | BBB21    | 6666
 666601  | Test2  | Satisfactory   | BBB21    | 6666

查询1

Select distinct sample ID 
where Microcomment = 'Unsatisfactory'

查询2

Select Distinct Sample ID 
where Microcomment = 'Satisfactory' 
  and (only display sampleID’s that don't appear in the Query 1)

查询3

Select Distinct Sample ID 
where Microcomment = 'No Test' 
  and (only display sampleID’s that don't appear in the Query 1 & 2)

我试图实现的最终输出

SampleID | Microcomments   | ProductID
---------+-----------------+----------
 111101  | Unsatisfactory  | ABC123
 222202  | Unsatisfactory  | A21
 333301  | Satisfactory    | AB11
 444403  | Unsatisfactory  | ABCD123
 555504  | Unsatisfactory  | BA123
 666601  | Satisfactory    | BBB21

请注意,这不是实际数据

3 个答案:

答案 0 :(得分:2)

如果您的DBMS支持它,那么我认为最简单的方法是使用微观元素对每个样本中的行进行排名:

SELECT  SampleID, TestID, MicroComments, ProductID, CompanyID
FROM    (   SELECT  SampleID,
                    TestID,
                    MicroComments,
                    ProductID,
                    CompanyID,
                    ROW_NUMBER() OVER(PARTITION BY SampleID 
                                        ORDER BY CASE WHEN MicroComments = 'UnSatisfactory' THEN 1
                                                        WHEN MicroComments = 'Satisfactory' THEN 2
                                                        WHEN MicroComments = 'No test' THEN 3
                                                    END) AS RowNum
            FROM    T
        ) T
WHERE   RowNum = 1;

如果你的DBMS不支持它(或者即使它支持它),你也可以使用条件聚合来获得它。这是基于以下假设:对于每个SampleID,ProductID和CompanyID都是常量:

SELECT  SampleID, ProductID, CompanyID,
        CASE WHEN COUNT(CASE WHEN MicroComments = 'UnSatisfactory' THEN 1 END) > 0 THEN 'UnSatisfactory'
            WHEN COUNT(CASE WHEN MicroComments = 'Satisfactory' THEN 1 END) > 0 THEN 'Satisfactory'
            WHEN COUNT(CASE WHEN MicroComments = 'No test' THEN 1 END) > 0 THEN 'No test'
        END AS MicroComments
FROM    T
GROUP BY SampleID, ProductID, CompanyID;

<强> Examples on DB Fiddle

答案 1 :(得分:1)

我认为你让它比需要的更难......

再次编辑

尝试以下内容:

-- Final output:
SELECT SampleID, MAX(Microcomments), ProductID
FROM table
GROUP BY SampleID, ProductID

只要它具有UnsatisfactorySatisfactoryNo Test值,按字母顺序排序,此顺序就是您要为SampleID显示的顺序...

答案 2 :(得分:0)

<强>更新

好的,既然我已经更好地理解了你想要实现的目标,我已经修改了查询。

关键是联合三个不同的结果集。通过使用NOT IN子句排除其他相关集合来创建每个集合。

SELECT DISTINCT(t1.SampleID), t1.Microcomments, t1.ProductID
FROM t_table t1
WHERE t1.Microcomments = 'Unsatisfactory'
UNION
SELECT DISTINCT(t2.SampleID), t2.Microcomments, t2.ProductID
FROM t_table t2
WHERE t2.Microcomments = 'Satisfactory' AND t2.SampleID NOT IN (SELECT SampleID from t_table WHERE Microcomments = 'Unsatisfactory')
UNION
SELECT DISTINCT(t3.SampleID), t3.Microcomments, t3.ProductID
FROM t_table t3
WHERE t3.Microcomments = 'No Test' AND t3.SampleID NOT IN (SELECT SampleID from t_table WHERE Microcomments IN('Satisfactory','Unsatisfactory'))
ORDER BY SampleID ASC;

我在这里创建了一个sql小提琴:http://sqlfiddle.com/#!15/3204f/19

如果您正在寻找,请告诉我。

- 原始 -

您可以使用WHERE子句中的OR语句将要搜索的所有条件组合到单个查询中。除非数据比您显示的更复杂,否则这对您有用。

SELECT DISTINCT SampleID, Microcomments, ProductID
FROM table_name 
WHERE Microcomments = ‘Satisfactory’ OR Microcomments = ‘Satisfactory’ OR Microcomments = ‘No Test’ 
ORDER BY Microcomments