简体   繁体   English

SQL 过滤独特的结果

[英]SQL Filter unique results

I am trying to get an SQL statement that will output a unique part number eg no duplicates However I want the type as Purchased is the default and when there isnt a Purchased part it defults back to Manufactured.我正在尝试获取 SQL 语句,该语句将为 output 提供唯一的零件编号,例如没有重复但是我希望类型为“已购买”是默认值,当没有已购买的零件时,它会返回“制造”。 NOTE all parts can be purchased注意所有零件都可以购买

The result I require is to only show unique part numbers eg 1 to 10 and Purchased is the default Type我需要的结果是只显示唯一的零件编号,例如 1 到 10,并且 Purchased 是默认类型

Table1表格1

Part_number | Type         | Cost
Part 1 |    Manufactured   | £1.00
Part 1 |    Purchased      | £0.56
Part 2 |    Manufactured   | £1.26
Part 2 |    Purchased      | £0.94
Part 3 |    Manufactured   | £0.36
Part 3 |    Purchased      | £0.16
Part 4 |    Manufactured   | £1.00 
Part 4 |    Purchased      | £1.50
Part 5 |    Manufactured   | £1.65
Part 6 |    Manufactured   | £1.98 
Part 7 |    Manufactured   | £0.15
Part 8 |    Manufactured   | £0.45
Part 9 |    Manufactured   | £1.20
Part 9 |    Purchased      | £0.80
Part 10|    Manufactured   | £1.00

This is the result I am hoping to get back这是我希望回来的结果

Part_number | Type         | Cost
Part 1 |    Purchased      | £0.56
Part 2 |    Purchased      | £0.94
Part 3 |    Purchased      | £0.16
Part 4 |    Purchased      | £1.50
Part 5 |    Manufactured   | £1.65
Part 6 |    Manufactured   | £1.98 
Part 7 |    Manufactured   | £0.15
Part 8 |    Manufactured   | £0.45
Part 9 |    Purchased      | £0.80
Part 10|    Manufactured   | £1.00

I have tried loads of different techniques but am not getting the result.我尝试了很多不同的技术,但没有得到结果。

I am guessing that I will need to create temp tables that are filtered and then join the tables together but I really don't know.我猜我需要创建经过过滤的临时表,然后将这些表连接在一起,但我真的不知道。

Any help will be apricated任何帮助都会得到帮助

One method uses not exists .一种方法使用not exists Assuming you have at most two rows per part:假设每个部分最多有两行:

select t.*
from t
where t.type = 'Purchased' or
      (t.type = 'Manufactured' and
       not (exists (select 1 from t t2 where t2.part_number = t.part_number and t2.type = 'Purchased')
           )
      );

There are other fun ways to handle this.还有其他有趣的方法来处理这个问题。 For instance, an aggregation approach:例如,聚合方法:

select part_number,
       max(type) as type,
       coalesce(max(case when type = 'Purchased' then cost end),,
                max(cost)
               ) as cost
from t
group by part_number;

You could also just grab the first row in each group by sorting them.您也可以通过对它们进行排序来获取每个组中的第一行。 This would make it easier when there are other columns of data to bring back.当有其他数据列要带回时,这将更容易。

with data as (
    select *, row_number() over (
        partition by part_number
        order by case when t.type = Purchased then 1 else 2 end) as rn
    from t
)
select * from data where rn = 1;

If there are other types this would work as well although you would want to tweak it if there are more than two per part.如果还有其他类型,这也可以,但如果每个部分有两个以上,您可能需要对其进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM