Note that there are some explanatory texts on larger screens.

plurals
  1. POpivot to total per week
    primarykey
    data
    text
    <p>I have a query that i am trying to write to return the amount of times and instance occurs in a database and group by week and product as defined by the database. below is my query:</p> <pre><code>SELECT WeekCompleted, Product, COUNT(OrderNumber) as CorrectionCount FROM( SELECT p.Abbreviation as Product ,CAST(oi.OrderID as VARCHAR(MAX))+'.'+CAST(oi.OrderItemID as VARCHAR(MAX)) AS OrderNumber ,CASE WHEN o.ClientID IN (56156, 56394) THEN DATEADD(week, datepart(ww, dbo.GetLatestMilestoneDate(oi.OrderID, oi.OrderItemID, 80)) - 1, DATEADD(DAY, @@datefirst - DATEPART(weekday, CAST(YEAR(GETDATE()) AS VARCHAR) + '-01-01') - 6, CAST(YEAR(GETDATE()) AS VARCHAR) + '-01-01')) ELSE DATEADD(week, datepart(ww, dbo.GetLatestMilestoneDate(oi.OrderID, oi.OrderItemID, 130)) - 1, DATEADD(DAY, @@datefirst - DATEPART(weekday, CAST(YEAR(GETDATE()) AS VARCHAR) + '-01-01') - 6, CAST(YEAR(GETDATE()) AS VARCHAR) + '-01-01')) END AS WeekCompleted ,CASE WHEN o.ClientID IN (56156,56394) THEN dbo.GetLatestMilestoneDate(oi.OrderID, oi.OrderItemID, 80) ELSE dbo.GetLatestMilestoneDate(oi.OrderID, oi.OrderItemID, 130) END AS LastCompleted FROM OrderItems oi LEFT JOIN OrderItemMilestones oim on oim.OrderID = oi.OrderID and oim.OrderItemID = oi.OrderItemID JOIN Products p on p.ProductID = oi.ProductID JOIN Orders o on o.OrderID = oi.OrderID WHERE oim.MilestoneID = 90 and QueueID = 0 ) src WHERE LastCompleted &gt;= '2012-10-01' GROUP BY WeekCompleted, Product </code></pre> <p>here is more info needed</p> <pre><code> dbo.getlatestmilestonedate() returns a datetime date in this format: mm:dd:yyyy hh:mm:ss </code></pre> <p>and a sample table of data i currently have:</p> <pre><code>WeekCompleted Product CorrectionCount 2012-09-30 00:00:00.000 Product1 5 2012-10-07 00:00:00.000 Product1 7 2012-10-14 00:00:00.000 Product1 7 2012-09-30 00:00:00.000 Product2 18 2012-10-07 00:00:00.000 Product2 28 2012-10-14 00:00:00.000 Product2 16 </code></pre> <p>This data is returning exactly how i want it to so no change is needed to the original data unless needed to accomplish final goal, which is this:</p> <pre><code>WeekCompleted Product1 Product2 2012-09-30 00:00:00.000 5 18 2012-10-07 00:00:00.000 7 28 2012-10-14 00:00:00.000 7 16 </code></pre> <p>I think i need to pivot this, but every time i try i only run into syntax errors and dont quite yet understand the pivot syntax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload