Wednesday, March 28, 2012

Query Help

When I run this query it outputs the dates from the 14th of Nov to the 31st (well up to the last date in the db which atm is the 6th of dec) twice for each date because we have two order status values for each day.. Which is what I would like it to do..

However I would like to make it so if a particular row does not have a value <> 0 in any of the Orders fields that row is not displayed hence only displaying days/orderstatuses with actual orders and not days with 0's in all the COUNT(CASE WHEN discountoffercode = '##' THEN 1 END) Fields... I have tried so many things and I just cant figure out how to do this

Code: ( sql )

    SELECT ReceivedDate, OrderStatus, COUNT(CASE WHEN discountoffercode = '88' THEN 1 END) AS OrdersCisco, SUM(CASE WHEN discountoffercode = '88' THEN ordertotal END) AS SalesCisco, COUNT(CASE WHEN discountoffercode = '89' THEN 1 END) AS OrdersTDS, SUM(CASE WHEN discountoffercode = '89' THEN ordertotal END) AS SalesTDS, COUNT(CASE WHEN discountoffercode = '90' THEN 1 END) AS OrdersBerbeeCDW, SUM(CASE WHEN discountoffercode = '90' THEN ordertotal END) AS SalesBerbeeCDW, COUNT(CASE WHEN discountoffercode = '91' THEN 1 END) AS OrdersQuadGfx, SUM(CASE WHEN discountoffercode = '91' THEN ordertotal END) AS SalesQuadGfx, COUNT(CASE WHEN discountoffercode = '92' THEN 1 END) AS OrdersATT, SUM(CASE WHEN discountoffercode = '92' THEN ordertotal END) AS SalesATT, COUNT(CASE WHEN discountoffercode = '93' THEN 1 END) AS OrdersGlobalCrossing, SUM(CASE WHEN discountoffercode = '93' THEN ordertotal END) AS SalesGlobalCrossing, COUNT(CASE WHEN discountoffercode = '94' THEN 1 END) AS OrdersPerformics, SUM(CASE WHEN discountoffercode = '94' THEN ordertotal END) AS SalesPerformics, COUNT(CASE WHEN discountoffercode = 'AA' THEN 1 END) AS OrdersOzburnHessey, SUM(CASE WHEN discountoffercode = 'AA' THEN ordertotal END) AS SalesOzburnHessey, COUNT(CASE WHEN discountoffercode = 'AB' THEN 1 END) AS OrdersFry, SUM(CASE WHEN discountoffercode = 'AB' THEN ordertotal END) AS SalesFry, COUNT(CASE WHEN discountoffercode = 'AC' THEN 1 END) AS OrdersEMC, SUM(CASE WHEN discountoffercode = 'AC' THEN ordertotal END) AS SalesEMC, COUNT(CASE WHEN discountoffercode = 'AD' THEN 1 END) AS OrdersGlasshouse, SUM(CASE WHEN discountoffercode = 'AD' THEN ordertotal END) AS SalesGlasshouse, COUNT(CASE WHEN discountoffercode = 'AE' THEN 1 END) AS OrdersSecureWorks, SUM(CASE WHEN discountoffercode = 'AE' THEN ordertotal END) AS SalesSecureWorks, COUNT(CASE WHEN discountoffercode = 'AF' THEN 1 END) AS OrdersPDS, SUM(CASE WHEN discountoffercode = 'AF' THEN ordertotal END) AS SalesPDS, COUNT(CASE WHEN discountoffercode = 'AG' THEN 1 END) AS OrdersMenashaPkg, SUM(CASE WHEN discountoffercode = 'AG' THEN ordertotal END) AS SalesMenashaPkg, COUNT(CASE WHEN discountoffercode = 'A2' THEN 1 END) AS OrdersBelmark, SUM(CASE WHEN discountoffercode = 'A2' THEN ordertotal END) AS SalesBelmark, COUNT(CASE WHEN discountoffercode = 'A3' THEN 1 END) AS OrdersPlasticIngeniuty, SUM(CASE WHEN discountoffercode = 'A3' THEN ordertotal END) AS SalesPlasticIngenuity, COUNT(CASE WHEN discountoffercode = 'A4' THEN 1 END) AS OrdersUFP, SUM(CASE WHEN discountoffercode = 'A4' THEN ordertotal END) AS SalesUFP, COUNT(CASE WHEN discountoffercode = 'A5' THEN 1 END) AS OrdersStyrene, SUM(CASE WHEN discountoffercode = 'A5' THEN ordertotal END) AS SalesStyreneFROM dbo.OrdersWHERE (ReceivedDate BETWEEN '20071114' AND '20071231')GROUP BY ReceivedDate, OrderStatus
Thanks for the edit :P

um anyone able to help me on this? It is kind of high priority and as i said i can't seem to figure it outsql

No comments:

Post a Comment