我对SQL Server查询有疑问。
表格可以描述如下:
供应商(SID, sName, status, sCity
),PK = SID
零件(PID, pName, color, weight, pCity
),PK = PID
项目(JID, jName, city
),PK = JID
SPJ (SID, PID, JID, quantity
),PK =(SID,PID,JID),FK = SID,PID,JID
我正在尝试将同一部件的供应商的供应商编号和名称提供给至少两个项目,这是我对该任务的查询:
SELECT
s1.SID, s1.sName, COUNT(*)
FROM
Suppliers s1, Parts p, SPJ s2
WHERE
s1.SID = s2.SID
AND p.PID = s2.PID
GROUP BY
s1.SID, s1.sName
HAVING
COUNT(*)>1
ORDER BY
s1.SID, s1.sName
请帮我更正此查询。提前谢谢
答案 0 :(得分:2)
这正是你所要求的:
SELECT
S.SupplierID,
S.sName
FROM
dbo.Suppliers S
WHERE
EXISTS (
SELECT *
FROM dbo.SPJ SPJ
WHERE S.SupplierID = SPJ.SupplierID
GROUP BY SPJ.PartID
HAVING Count(*) >= 2
);
Please see my SQLFiddle在真实数据集上试用它。请注意,还有一个查询显示参与使供应商符合资格的所有各行(请参阅Qualifies
列)。以下是安装脚本的预览:
CREATE TABLE dbo.Suppliers (
SupplierID smallint identity(1,1) NOT NULL CONSTRAINT PK_Suppliers PRIMARY KEY CLUSTERED,
sName varchar(40) NOT NULL CONSTRAINT UQ_Suppliers_sName UNIQUE
);
CREATE TABLE dbo.Parts (
PartID int identity(1,1) NOT NULL CONSTRAINT PK_Parts PRIMARY KEY CLUSTERED,
pName varchar(100) NOT NULL CONSTRAINT UQ_Parts_pName UNIQUE
);
CREATE TABLE dbo.Projects (
ProjectID int identity(1,1) NOT NULL CONSTRAINT PK_Projects PRIMARY KEY CLUSTERED,
jName varchar(50) NOT NULL CONSTRAINT UQ_Projects_jName UNIQUE
);
CREATE TABLE dbo.SPJ (
SupplierID smallint NOT NULL CONSTRAINT FK_SPJ_Suppliers FOREIGN KEY REFERENCES dbo.Suppliers (SupplierID),
PartID int NOT NULL CONSTRAINT FK_SPJ_Parts FOREIGN KEY REFERENCES dbo.Parts (PartID),
ProjectID int NOT NULL CONSTRAINT FK_SPJ_Projects FOREIGN KEY REFERENCES dbo.Projects (ProjectID),
CONSTRAINT PK_SPJ PRIMARY KEY CLUSTERED (SupplierID, PartID, ProjectID)
);
INSERT dbo.Suppliers
VALUES
('Joe'' Specialty Goods'),
('Major Supplier'),
('Small Supplier'),
('Tamandua Manufacturing, Ltd.'),
('Things ''n'' Stuff'),
('Stuff ''n'' Things');
INSERT dbo.Parts
VALUES
('Watchamacallit'),
('Thingamajiggy'),
('Thingamabob'),
('Doohickey'),
('Whatsisname'),
('Thingy'),
('Gizmo'),
('Gadget'),
('Widget'),
('Titanium Widget'),
('Electronic Thingamabob'),
('One-Product Patrick');
INSERT dbo.Projects
VALUES
('Video Game Manufacturing'),
('Autodoc Research'),
('Consumer Electronics Expansion'),
('Space Shuttle'),
('Tablet Project Cobalt');
INSERT dbo.SPJ
VALUES
(1, 2, 1),
(1, 3, 1),
(1, 7, 1),
(1, 11, 1),
(2, 1, 1),
(2, 1, 2),
(2, 1, 3),
(2, 1, 4),
(2, 2, 1),
(2, 2, 2),
(2, 2, 3),
(2, 2, 4),
(2, 2, 5),
(2, 10, 1),
(2, 10, 2),
(2, 10, 3),
(2, 10, 4),
(2, 10, 5),
(3, 5, 4),
(4, 4, 2),
(4, 4, 3),
(5, 9, 1),
(5, 8, 2),
(5, 7, 3),
(5, 6, 4),
(5, 5, 5),
(6, 1, 1),
(6, 2, 1),
(6, 3, 1),
(6, 4, 1),
(6, 5, 1),
(6, 6, 1),
(6, 7, 1),
(6, 8, 1),
(6, 9, 1),
(6, 10, 1),
(6, 11, 1);
答案 1 :(得分:0)
SELECT SID, PID, COUNT(JID)
FROM SPJ
GROUP BY SID, PID
HAVING COUNT(JID) > 1
答案 2 :(得分:0)
你也可以这样做:
SELECT DISTINCT
a.SupplierID,
b.sName
FROM
(
SELECT
SupplierID,
PartID,
NumProjects = count(DISTINCT ProjectID)
FROM SPJ
GROUP BY
SupplierID, PartID
HAVING
count(DISTINCT ProjectID) >= 2
) a
INNER JOIN Suppliers b ON a.SupplierID = b.SupplierID
答案 3 :(得分:-1)
SELECT Supplier.SID,Supplier.sName
FROM SPJ inner join Supplier on SPJ.SID = Supplier.SID
inner join Parts on SPJ.PID = Parts.PID
GROUP BY Supplier.SID,Supplier.sName
HAVING COUNT(JID) > 1