2017-03-17 1 views
0

Was ich versuche zu tun, ist ein Bündel von Daten aus einer Tabelle (Passbooking) in 4 Spalten zusammenfassen. Die erste ist einfach eine Client-ID, und die nächsten drei sind eine Anzahl von Buchungen, die bestimmte Kriterien für jede Client-ID erfüllen.Joining 3 Tabellen von count abgeleitet und gruppieren von Anweisungen

Die dritte (OnDemandCancels) und vierte (DayCenterCancels) sind Teilmengen der zweiten (TotalCancels), und daher einige der Zeilen in der dritten und vierten Spalte sollte Null sein.

Aus diesem Grund denke ich, ich muss die Clientid für jede der Spalten enthalten, wenn sie von der ursprünglichen Tabelle abgeleitet sind, so dass ich sie entlang Clientid verbinden kann.

Hier ist so nah wie ich in der Lage gewesen zu bekommen:

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 

Das gibt mir eine Fehlermeldung „Der mehrteilige Bezeichner tcxl.clientid nicht gebunden werden kann“.

Ich weiß, dass jede der Unterabfragen so funktioniert, wie ich sie auf eigene Faust will, mein Problem ist nur herauszufinden, wie man sie richtig verbindet.

Danke!

+0

Welche Datenbank-Software verwenden Sie? MySQL, Postgres? – Ben

Antwort

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

überspringen die JOIN, verwenden case Ausdrücke statt bedingte Zählen zu tun:

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 

EDIT - wie gewünscht:

"was ist, wenn ich brauchte diese Tabelle nun beitreten eine andere Tabelle (Ich muss die Client - Namen aus einer anderen Tabelle beziehen) Wo kann ich diese Join - Anweisung hier einfügen? "

einfach JOIN die obige Abfrage mit der anderen Tabelle (genannt ClientsTable unten):

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

Das ist perfekt und um ehrlich zu sein. Vielen Dank. Nun, was wäre, wenn ich jetzt diese Tabelle mit einer anderen Tabelle verbinden müsste (ich muss die Client-Namen aus einer anderen Tabelle holen) Wo füge ich diese Join-Anweisung hier hinzu? – user37745