Hi
This is a kind of follow onto to a previous post but the problem is slightly different. I have adapted the code form the previous post to try and get a list od dates in the left hand column and then across the top a list of reasons why delivery's may have failed. The code appears to be working okay other than putting rows for each reason with the same date.
So for example if on the 2016-04-01 I have counts in OTIF (10), Other (5) and Admin (5)
It is listing the count separately with the headings across the top.
2016-04-01 10
2016-04-01 5
2016-04-01 5
What I am trying to get is
DeliveryDate Otif Other Admin Total
2016-04-01 10 5 5 20
I have tried to alter the code but cant seem to get the result I need. Could someone advise how and where I need to change the code please. Thanks in advance
; With Data As
(
SELECT count(oh.udfSalesOrderOTIF) AS Count
, oh.OrderStatus
, oh.Deliverydate
, oh.udfSalesOrderOTIF
FROM dbrderHeader oh
--INNER JOIN dbo.SalesRep sr
--ON oh.SalesRepID = sr.SalesRepID
WHERE DeliveryDate >= '2016-04-01 00:00:00'
GROUP BY oh.deliverydate
, oh.OrderStatus
, oh.udfSalesOrderOTIF
HAVING (oh.OrderStatus = 8)
AND (NOT (COUNT(oh.udfSalesOrderOTIF) IS NULL))
AND (NOT (oh.udfSalesOrderOTIF IS NULL))
), GrandTotal As
(
Select Sum([Count]) As GrandTotal
From Data
)
Select DeliveryDate
, Coalesce(Min(Case When udfSalesOrderOTIF = 'OTIF' Then [Count] End), 0) As [OTIF]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Early Delivery' Then [Count] End), 0) As [Early Delivery]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Make-up' Then [Count] End), 0) As [Make-up]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Not on system' Then [Count] End), 0) As [Not on system]
, Coalesce(Min(Case When udfSalesOrderOTIF= 'Other' Then [Count] End), 0) As [Other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Production - not processed' Then [Count] End), 0) As [Production - not processed]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Production - other' Then [Count] End), 0) As [Production - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Sales - cancelled order' Then [Count] End), 0) As [Sales - cancelled order]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Administration' Then [Count] End), 0) As [Admin]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Sales - other' Then [Count] End), 0) As [Sales - other]
, Coalesce(MIN(Case When udfSalesOrderOTIF = 'Transport - planned to fail' Then [Count] End), 0) As [Transport - planned to fail]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - insufficient room on trailer' Then [Count] End), 0) As [Transport - insufficient room on trailer]
, Coalesce(Min(Case When udfSalesOrderOTIF= 'Transport - delayed' Then [Count] End), 0) As [Transport - delayed]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - other' Then [Count] End), 0) As [Transport - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - vehicle broke down' Then [Count] End), 0) As [Production - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - weong product delivered' Then [Count] End), 0) As [Transport - weong product delivered]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - lost' Then [Count] End), 0) As [Transport - lost]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - no stock' Then [Count] End), 0) As [Yard - no stock]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - other' Then [Count] End), 0) As [Yard - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - product missing /lost' Then [Count] End), 0) As [Yard - product missing /lost]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - rolled / damaged' Then [Count] End), 0) As [Yard - rolled / damaged]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - wrong product delivered' Then [Count] End), 0) As [Yard - wrong product delivered]
, Sum([Count]) As [Total]
, 100.0 * Sum([count]) / GrandTotal.GrandTotal AS Percentage
From Data
Cross Join GrandTotal
Group By Deliverydate, udfSalesOrderOTIF,GrandTotal.GrandTotal
Order By Deliverydate
This is a kind of follow onto to a previous post but the problem is slightly different. I have adapted the code form the previous post to try and get a list od dates in the left hand column and then across the top a list of reasons why delivery's may have failed. The code appears to be working okay other than putting rows for each reason with the same date.
So for example if on the 2016-04-01 I have counts in OTIF (10), Other (5) and Admin (5)
It is listing the count separately with the headings across the top.
2016-04-01 10
2016-04-01 5
2016-04-01 5
What I am trying to get is
DeliveryDate Otif Other Admin Total
2016-04-01 10 5 5 20
I have tried to alter the code but cant seem to get the result I need. Could someone advise how and where I need to change the code please. Thanks in advance
; With Data As
(
SELECT count(oh.udfSalesOrderOTIF) AS Count
, oh.OrderStatus
, oh.Deliverydate
, oh.udfSalesOrderOTIF
FROM dbrderHeader oh
--INNER JOIN dbo.SalesRep sr
--ON oh.SalesRepID = sr.SalesRepID
WHERE DeliveryDate >= '2016-04-01 00:00:00'
GROUP BY oh.deliverydate
, oh.OrderStatus
, oh.udfSalesOrderOTIF
HAVING (oh.OrderStatus = 8)
AND (NOT (COUNT(oh.udfSalesOrderOTIF) IS NULL))
AND (NOT (oh.udfSalesOrderOTIF IS NULL))
), GrandTotal As
(
Select Sum([Count]) As GrandTotal
From Data
)
Select DeliveryDate
, Coalesce(Min(Case When udfSalesOrderOTIF = 'OTIF' Then [Count] End), 0) As [OTIF]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Early Delivery' Then [Count] End), 0) As [Early Delivery]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Make-up' Then [Count] End), 0) As [Make-up]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Not on system' Then [Count] End), 0) As [Not on system]
, Coalesce(Min(Case When udfSalesOrderOTIF= 'Other' Then [Count] End), 0) As [Other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Production - not processed' Then [Count] End), 0) As [Production - not processed]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Production - other' Then [Count] End), 0) As [Production - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Sales - cancelled order' Then [Count] End), 0) As [Sales - cancelled order]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Administration' Then [Count] End), 0) As [Admin]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Sales - other' Then [Count] End), 0) As [Sales - other]
, Coalesce(MIN(Case When udfSalesOrderOTIF = 'Transport - planned to fail' Then [Count] End), 0) As [Transport - planned to fail]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - insufficient room on trailer' Then [Count] End), 0) As [Transport - insufficient room on trailer]
, Coalesce(Min(Case When udfSalesOrderOTIF= 'Transport - delayed' Then [Count] End), 0) As [Transport - delayed]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - other' Then [Count] End), 0) As [Transport - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - vehicle broke down' Then [Count] End), 0) As [Production - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - weong product delivered' Then [Count] End), 0) As [Transport - weong product delivered]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Transport - lost' Then [Count] End), 0) As [Transport - lost]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - no stock' Then [Count] End), 0) As [Yard - no stock]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - other' Then [Count] End), 0) As [Yard - other]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - product missing /lost' Then [Count] End), 0) As [Yard - product missing /lost]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - rolled / damaged' Then [Count] End), 0) As [Yard - rolled / damaged]
, Coalesce(Min(Case When udfSalesOrderOTIF = 'Yard - wrong product delivered' Then [Count] End), 0) As [Yard - wrong product delivered]
, Sum([Count]) As [Total]
, 100.0 * Sum([count]) / GrandTotal.GrandTotal AS Percentage
From Data
Cross Join GrandTotal
Group By Deliverydate, udfSalesOrderOTIF,GrandTotal.GrandTotal
Order By Deliverydate