我想要做的是将一个表(存折)的一堆数据汇总为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无法绑定”。
我知道每个子查询的功能都是我希望他们自己去做的,我的问题就在于弄清楚如何正确地加入它们。
谢谢!
的数据库软件,您使用的? MySQL,Postgres? – Ben