Databases Reference
In-Depth Information
■
12.2■Bestandskunden pro Monat
Mit dem vorherigen Select ist der Grundstein gelegt. Wir können nun recht einfach die
Anzahl der Bestandskunden pro Monat errechnen.
Select
DATEPART (yyyy,a.postingDate) Jahr, →
Ausgabe der Jahreszahl
DATEPART (mm,a.postingDate) Monat, →
Ausgabe des Monats
COUNT (distinct a.riskID) Bestandskunden →
Anzahl der Bestandskunden
from
(SELECT c.riskID, s.postingDate
FROM [dbo].[iw_sales]s,[dbo].[iw_customer]c
where s.customerNo = c.customerNo)a,
(SELECT c.riskID, min(s.postingDate)firstOrder
FROM [dbo].[iw_sales]s,[dbo].[iw_customer]c
where s.customerNo = c.customerNo
group by c.riskID)b
where a.riskID = b.riskID →
Join von Abfrage a und Abfrage b
and a.postingDate > b.firstOrder →
Bedingung
group by DATEPART (yyyy,a.postingDate), DATEPART (mm,a.postingDate)
→
Group by-Klausel
order by DATEPART (yyyy,a.postingDate), DATEPART (mm,a.postingDate)
→
Order by-Klausel
Nun haben wir die Anzahl der Bestandskunden pro Monat ermittelt (Bild 12.2).
Bild 12.2■
Anzahl der Bestandskunden
pro Monat