2017-03-17 101 views
0

我想要做的是将一个表(存折)的一堆数据汇总为4列。第一个是简单的,后面三个是符合每个CLIEntid标准的预订数量。加入由表计数和组的3个表格

第三个(OnDemandCancels)和第四个(DayCenterCancels)是第二个(TotalCancels)的子集,因此第三个和第四个列中的某些行应该为零。

因为这个原因,我想我需要包括每个列的clientid,当他们从原始表派生,以便我可以沿着clientid加入他们。

这是接近我已经能够得到:

select 
    pb.clientid, 
    (select pb.clientid, count(pb.ldate) as TotalCancels 
     from passbooking as pb 
     where pb.ldate >= 20170201 
      and pb.ldate <= 20170228 
      and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
      group by pb.clientid) as tcxl, 
    (select pb.clientid, count(pb.ldate) as OnDemandCancels 
     from passbooking as pb 
     where pb.ldate >= 20170201 
      and pb.ldate <= 20170228 
      and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
      and pb.bookingpurpose <> 'P-DayCt') 
      group by pb.clientid) as odcxl, 
    (select pb.clientid, count(pb.ldate) as DayCenterCancels 
     from passbooking as pb 
     where pb.ldate >= 20170201 
      and pb.ldate <= 20170228 
      and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
      and pb.bookingpurpose = 'P-DayCt') 
      group by pb.clientid) as dccxl 
from passbooking as pb 
where pb.clientid = tcxl.clientid 
    and pb.clientid = odcxl.clientid 
    and pb.clientid = dccxl.clientid 

这给了我一个错误“多部分组成的标识符tcxl.clientid无法绑定”。

我知道每个子查询的功能都是我希望他们自己去做的,我的问题就在于弄清楚如何正确地加入它们。

谢谢!

+0

的数据库软件,您使用的? MySQL,Postgres? – Ben

回答

0
select 
    pb.clientid 
from passbooking as pb 
inner join ( 
      (select pb.clientid, count(pb.ldate) as TotalCancels 
       from passbooking as pb 
       where pb.ldate >= 20170201 
       and pb.ldate <= 20170228 
       and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
       group by pb.clientid) 
       ) as tcxl 
on pb.clientid = tcxl.clientid 
inner join (
      (select pb.clientid, count(pb.ldate) as OnDemandCancels 
       from passbooking as pb 
       where pb.ldate >= 20170201 
       and pb.ldate <= 20170228 
       and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
       and pb.bookingpurpose <> 'P-DayCt') 
       group by pb.clientid) 
      )as odcxl 
on pb.clientid = odcxl.clientid 

inner join 
(
    (select pb.clientid, count(pb.ldate) as DayCenterCancels 
    from passbooking as pb 
    where pb.ldate >= 20170201 
    and pb.ldate <= 20170228 
    and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
    and pb.bookingpurpose = 'P-DayCt') 
    group by pb.clientid) 
)as dccxl 
on pb.clientid = dccxl.clientid 
1

跳过JOIN,使用case表达式来完成,而不是有条件的计数:

select 
    pb.clientid, 
    count(pb.ldate) as TotalCancels, 
    count(case when pb.bookingpurpose <> 'P-DayCt' then pb.ldate end) as OnDemandCancels, 
    count(case when pb.bookingpurpose = 'P-DayCt' then pb.ldate end) as DayCenterCancels 
from passbooking as pb 
where pb.ldate >= 20170201 
    and pb.ldate <= 20170228 
    and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
group by pb.clientid 

编辑 - 的要求:

如果我需要什么,现在就加入此表另一个表(我需要从不同的表中获取客户端名称)在哪里添加该连接语句?

只需JOIN与其他表(以下称为ClientsTable)上面的查询:

select b.clientid, b.TotalCancels, b.OnDemandCancels, b.DayCenterCancels, c.clientname 
from 
(
    select 
     pb.clientid, 
     count(pb.ldate) as TotalCancels, 
     count(case when pb.bookingpurpose <> 'P-DayCt' then pb.ldate end) as OnDemandCancels, 
     count(case when pb.bookingpurpose = 'P-DayCt' then pb.ldate end) as DayCenterCancels 
    from passbooking as pb 
    where pb.ldate >= 20170201 
     and pb.ldate <= 20170228 
     and (pb.schedulestatus = 430 or pb.schedulestatus = 420) 
    group by pb.clientid 
) b 
JOIN ClientsTable c on b.clientid = c.clientid 
+0

这是完美的,说实话真棒。谢谢。 现在,如果我现在需要将此表连接到另一个表(我需要从另一个表中获取客户端名称),那么该怎么在这里添加该连接语句呢? – user37745