SELECT CustomerType
,Sum(Week0) AS [Week of 1 Mar]
,Sum(Week1) AS [Week of 8 Mar]
,Sum(Week2) AS [Week of 15 Mar]
FROM (
SELECT CustomerType
, CASE
WHEN i.InvoiceDate >= '03/02/2008' and i.InvoiceDate < '03/09/2008'
THEN (id.amount)
ELSE 0
END AS [Week0]
, CASE
WHEN InvoiceDate >= '03/09/2008' and InvoiceDate < '03/16/2008'
THEN (id.Amount)
ELSE 0
END AS [Week1]
, CASE
WHEN InvoiceDate >= '03/16/2008' and InvoiceDate < '03/23/2008'
THEN (id.Amount)
ELSE 0
END AS [Week2]
FROM tbInvoice i
INNER JOIN [tbInvoiceDetail] id ON i.guidInvoice = id.guidInvoice
INNER JOIN tbCustomer c on i.guidcustomer = c.guidcustomer
INNER JOIN tbCustomerTypeInfo ct on c.guidcustomertype = ct.guidcustomertype
WHERE (i.InvoiceDate >= '03/01/2008' AND i.InvoiceDate < '03/30/2008') and id.componentlevel = 0
) AS InnerDetail
GROUP BY customertype
Order by customertype
COMPUTE Sum(Sum(Week0)), Sum(Sum(Week1)), sum(sum(week2))
|