这是关于我的硬件分配的一个非常困难的 sql 查询,我有点卡住了。任何提示将不胜感激。
我的查询:
SELECT nest1.carname,
nest1.plndescription,
nest1.plancount,
nest2.totalmems,
Round(( nest1.plancount / nest2.totalmems ), 2) AS pct
FROM (SELECT carriers.carname,
p.plndescription,
Count(members.planid)AS plancount
FROM carriers,
plans p,
members
WHERE carriers.carrierid = p.carrierid
AND p.planid = members.planid
GROUP BY carriers.carname,
p.plndescription)nest1
NATURAL JOIN (SELECT carriers.carrierid,
Count(members.planid)AS totalmems
FROM carriers,
plans p,
members
WHERE carriers.carrierid = p.carrierid
AND p.planid = members.planid
GROUP BY carriers.carrierid)nest2
ORDER BY nest1.carname
我的表格和关系;
` CREATE TABLE Carriers
( CarrierID varchar2(4) not null,
carName varchar2(35),
carAddress varchar2(50),
carCity varchar2(30),
carStCode varchar2(2),
carZip varchar2(10),
carPhone varchar2(10),
carWebSite varchar2(255),
carContactFirstname varchar2(35),
carContactLastName varchar2(35),
carContactEmail varchar2(255),
CONSTRAINT pk_CarrierID PRIMARY KEY (CarrierID)
);`
CREATE TABLE Plans
( PlanID integer not null,
plnDescription varchar2(35),
plnCost decimal (8,2),
CarrierID varchar2(4),
CONSTRAINT pk_PlanID PRIMARY KEY (PlanID),
CONSTRAINT fk_CarrierID FOREIGN KEY (CarrierId) REFERENCES Carriers
);
CREATE TABLE Members
( MemberNo integer not null,
mbrFirstname varchar2(35),
mbrLastName varchar2(35),
mbrStreet varchar2(50),
mbrCity varchar2(30),
mbrState varchar2(2),
mbrZip varchar2(10),
mbrPhoneNo varchar2(10),
PlanID integer,
mbrEmail varchar2(255),
mbrDateEffective date,
employerID integer,
CONSTRAINT pk_MemberNo PRIMARY KEY (MemberNo),
CONSTRAINT fk_PlanID FOREIGN KEY (PlanId) REFERENCES Plans,
CONSTRAINT fk_employerID FOREIGN KEY (employerID) REFERENCES employers
);
问题:创建一个查询,该查询将列出所有运营商及其计划,以及一列显示该计划中的成员数量、运营商服务的成员总数以及该计划中运营商成员的百分比。例如 – Blue Cross Blue Shield – 将显示如下:
正确的输出:
运营商计划 PlanCount TotalMems Pct Blue Cross Blue Shield 2-Party Basic Medical 10 22 45.45