Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:29:34+00:00 2026-06-13T02:29:34+00:00

I have a query that i am trying to write to return the amount

  • 0

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:

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 >= '2012-10-01'
GROUP BY WeekCompleted, Product

here is more info needed

 dbo.getlatestmilestonedate() returns a datetime date in this format: mm:dd:yyyy hh:mm:ss

and a sample table of data i currently have:

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

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:

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:29:35+00:00Added an answer on June 13, 2026 at 2:29 am

    Try this

    select weekcompleted, product1, product2
    from (your query) src
    pivot (sum(CorrectionCount) for Product in ([Product1],[Product2])) p
    

    If your products can vary then you have to build up the pivot query above using dynamic SQL

    declare @columns varchar(4000)
    select @columns = '';
    select @columns = ',[' + productname +']' 
        from (select distinct productname from products) v
    select @columns = substring (@columns, 2, len(@columns))
    
    declare @sql nvarchar(4000)
    select @sql = 'select weekcompleted, ' + @columns 
    + ' from (your query) src '
    + ' pivot (sum(CorrectionCount) for Product in ('+@columns+')) p'
    
    exec sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to write a query that will return all users that have
I have a SQL query that I'm trying to write, but I'm not quite
I have a database on an external server that I am trying to query.
I'm trying to write a query that will return all QUERY_ID values alongside all
I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that
I'm trying to write a query that will return ... Each distinct user id
I have never used Informix before and I'm trying to write a query that
I am trying to write a query that will return what hosts are missing
I'm trying to write a query for a database that will left join a
I am trying to write a query that will return only the most recent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.