Heim  >  Fragen und Antworten  >  Hauptteil

Ist es möglich, per Join nach Spalten zu gruppieren?

select tc.UIDPK,
count(torder.status) as total
from TCUSTOMER tc
inner join TORDER torder on tc.UIDPK=torder.CUSTOMER_UID
where tc.UIDPK=490000;

Die obige Abfrage funktioniert bei mir einwandfrei. Der Status kann jedoch IN_PROGRESS, FAILED, ON_HOLD

lauten

Wie schreibe ich eine Abfrage, die eine Statusanzahl zurückgibt? wie tc.UIDPK, Gesamtbestellungen, IN_PROGRESS-Bestellungen, Gesamtbestellungen-IN_PROGRESS-Bestellungen. Ich habe Folgendes versucht, aber es hat nicht funktioniert

select tc.UIDPK,
count(torder.status) as total,
count(torder2.status) as inprogress,
count(torder.status)-count(torder2.status) as remaining
from TCUSTOMER tc
inner join TORDER torder on tc.UIDPK=torder.CUSTOMER_UID
left join TORDER torder2 on tc.UIDPK=torder2.CUSTOMER_UID and torder2.status in('IN_PROGRESS')
where tc.UIDPK=490000;

P粉702946921P粉702946921386 Tage vor541

Antworte allen(1)Ich werde antworten

  • P粉019353247

    P粉0193532472023-09-10 00:01:39

    不需要多次连接,使用SUM。

    尝试

    select tc.UIDPK,
           COUNT(torder.status) as total,
           SUM(torder.status = 'IN_PROGRESS') as inprogress,
           COUNT(torder.status) - SUM(torder.status = 'IN_PROGRESS') as remaining
    from TCUSTOMER tc
    inner join TORDER torder on tc.UIDPK=torder.CUSTOMER_UID
    where tc.UIDPK=490000
    group by tc.UIDPK;

    Antwort
    0
  • StornierenAntwort