0

这是关于我的硬件分配的一个非常困难的 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
4

2 回答 2

0

懒惰,不想写那么多。我没有检查这些解析是否正确,但由于这是家庭作业,它应该太容易了。

如果您使用的是 mysql,我会这样做并使用带有子查询的会话变量:

select c.carName As Carrier
     , p.plnDescription As Plan
     , count(1) As PlanCount
     , (select @plnCount:=count(1) from members m where m.PlanId=p.PlanId) TotalMems
     , (select @plnCount/count(1) from members) Pct
 from carriers c
 left join plans p on p.CarrierId=c.CarrierId

但是您的 ddl 表明您正在使用 oracle (b/c varchar2),所以更像是:

select c.carName As Carrier
     , p.plnDescription As Plan
     , PlanCount
     , PlanCount/tot Pct
 from carriers c
     ,plans p on p.CarrierId=c.CarrierId
     ,(select sum(decode(m.planId,p.planId,1,0)) PlanCount, count(1) tot from members)
于 2013-04-19T20:15:04.093 回答
0

我会做以下事情。最低查询只是对所有成员的查询,无论一个人在哪个计划中。这将需要在查询执行计划中自行运行,因为它将产生一条记录并应用于其余的每条记录查询。这导致没有实际的 JOIN 或 WHERE 子句。

接下来,from 的第一部分仅对按计划分组的每个特定计划的计划 ID 和成员计数进行预聚合。

现在,进入适当的 JOIN 子句以提取计划的详细信息以进行描述,然后是运营商名称的运营商,然后按您想要的任何方式订购...

SELECT
      C.CarName,
      P.PlnDescription,
      PerPlan.PerPlanCount,
      ( PerPlan.PerPlanCount / AllPlans.AllMembers ) as PcntOfAllMembers
   from
      ( select M.PlanID, COUNT(*) as PerPlanCount
           from Members M
           group by M.PlanID ) PerPlan
           Join Plans P
              ON PerPlan.PlanID = P.PlanID
              Join Carriers C
                 ON P.CarrierID = C.CarrierID,
      ( select COUNT(*) AllMembersCount
           from Members ) AllPlans 
   order by
      C.CarName,
      P.PlnDescription

您的样本中的计划计数为 10,但没有任何迹象表明“2 方基本医疗”有多个子计划,因此我认为这不是每个数据的真实列。

但是,如果 10 代表“Blue Cross”有多少不同的计划,而“2-Party...”是其中一个计划,那将是一个不同的查询。

于 2013-04-20T02:42:53.817 回答