我想要做的是將一個表(存摺)的一堆數據彙總爲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