SQL查询从两个嵌套的子查询中进行选择

时间:2013-04-19 19:44:43

标签: mysql sql

这是一个非常难的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
    );
问题: 创建一个查询,列出所有承运人及其计划,以及显示该计划中成员数量的列,承运人所服务的成员总数以及该计划中承运人成员的百分比。例如 - 蓝十字蓝盾 - 将显示如下:

正确输出:

Carrier                 Plan                    PlanCount  TotalMems  Pct
Blue Cross Blue Shield  2-Party Basic Medical   10         22         45.45

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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-Party Basic Medical”的多个子计划,因此我认为这不是每个数据的真实列。< / p>

但是,如果10表示'蓝十字'有多少不同的计划,而'2-Party ......'就是其中一个计划,那将是一个不同的查询。