简体   繁体   English

如何在SQL中为那些供应商提供的所有零件的供应商找到供应商编号?

[英]How to find the Supplier number for those suppliers who supply all parts that are supplied by another by supplier in SQL?

I am trying to find the SNO for the Supplier that supplied all parts that is supplied that by another by Supplier. 我正在寻找供应商的SNO,该供应商提供了由供应商提供的所有零件。

For example, I have 例如,我有

(S1, P1, 100), 
(S1, P2, 200), 
(S2, P1, 300), 
(S2, P2, 100),
(S3, P1, 400),
(S4, P1, 100), 
(S4, P2, 200)

So I am trying to find the SNO of the suppliers that supplied all the parts supplied by S2 not one part. 因此,我试图找到提供S2提供的所有零件的供应商的SNO,而不是一个零件。 Therefore, the answer should be S1 & S4 but I could not get the SQL Query to solve this. 因此,答案应该是S1和S4,但我无法通过SQL查询来解决此问题。 I hope I could get some help. 希望我能有所帮助。

this is how I achieved the result: 这是我达到结果的方式:

create table MyTable (
  Supplier varchar(20),
  Part varchar(20),
  SomeOrder int
)

insert into MyTable values 
('S1', 'P1', 100),
('S1', 'P2', 200), 
('S2', 'P1', 300), 
('S2', 'P2', 100),
('S3', 'P1', 400),
('S4', 'P1', 100), 
('S4', 'P2', 200)
---------------------------

-- temporary table to store all parts of a supplier
CREATE TABLE #tmp (Part varchar(20))

-- put your desired provider here
declare @Supplier varchar(20) = 'S2'

-- select all parts of this supplier
insert into #tmp
select Part from MyTable where Supplier = @Supplier

-- count how many parts this supplier has
declare @partCount int
select @partCount = count(*) from #tmp

-- select all suppliers that have all the parts (i.e. count = @partCount)
select innerTbl.Supplier from (
    select m.Supplier, m.Part
    from MyTable m
    inner join #tmp t on t.Part = M.Part
  ) innerTbl
 group by innerTbl.Supplier
 having Count(innerTbl.Supplier) = @partCount 
 and innerTbl.Supplier <> @Supplier

You can do this without temporary tables. 您可以在没有临时表的情况下执行此操作。 Assuming there are no duplicates in your table: 假设表中没有重复项:

select sp.supplier
from supplierparts sp join
     supplierparts sp2
     on sp.part = sp2.part and sp2.supplier = 2
group by sp.supplier
having count(*) = (select count(*) from supplierparts sp3 where sp3.supplier = 2);

If you wanted exact equality (that is, supplying P3 would invalidate the supplier), then: 如果您想要完全相等(即,提供P3将使供应商无效),则:

select sp.supplier
from supplierparts sp left join
     supplierparts sp2
     on sp.part = sp2.part and sp2.supplier = 2                    
group by sp.supplier
having count(*) = (select count(*) from supplierparts sp3 where sp3.supplier = 2) and
       count(*) = count(sp2.part);

暂无
暂无

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

相关问题 查找供应每个零件的供应商的供应商编号 - Find the Supplier number for those suppliers who supply every part SQL计算每个供应商提供的食物数量,即使他们没有供应任何食物 - SQL count the number of foods supplied by each supplier even if they did not supply anything 对于有 2 个或更多供应商的车辆,计算每辆车的零件数 (vehicle_part) 和供应商 (supplier) - For vehicles with 2 or more suppliers, COUNT the number of parts (vehicle_part) and suppliers (supplier) per vehicle 查找仅供应红色零件的供应商的Sid - Find the sids of suppliers who supply only red parts SQL Server 需要找到提供最不同产品的供应商 - SQL Server need to find suppliers who supply the most different products DB2 SQL查找提供特定颜色的供应商ID - DB2 SQL Find the Id of Suppliers, who supply specific color 获取所有供应商编号对,以便将两个供应商并置 - Get all pairs of supplier numbers such that two suppliers are colocated 查找供应每个零件的供应商的sid - Find the sids of the suppliers who supply every part 使用 SQL 查找美国供应商所有产品的平均价格 - Find the average price for all the products from the USA supplier Using SQL 查找某个部分的收费比该部分的平均成本高的供应商的价格(在所有提供该部分的供应商中平均) - find the sids of suppliers who charge more for some part than the average cost of that part (averaged over all the suppliers who supply that part)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM