2017-03-17 98 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