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 8474631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:40:53+00:00 2026-06-10T17:40:53+00:00

I have a table in SQL Server 2005 that I want to aggregate data

  • 0

I have a table in SQL Server 2005 that I want to aggregate data from, here is an example.

JobName       ProductionCounter       StartDate             EndDate               CounterName        Stage
----------------------------------------------------------------------------------------------------------

200           23                      2012-08-30 13:45:00                          Kilograms          Final
200           2                       2012-08-30 13:45:00                          Bars               Final
    [...]
200           46                      2012-08-30 13:45:00   2012-08-30 17:23:00    Kilograms          Final
200           4                       2012-08-30 13:45:00   2012-08-30 17:23:00    Bars               Final
300           20                      2012-08-30 13:45:00                          Kilograms          1st
300           10                      2012-08-30 18:12:00                          Bars               1st
    [...]
300           40                      2012-08-30 18:12:00   2012-08-30 19:17:00    Kilograms          1st
300           20                      2012-08-30 18:12:00   2012-08-30 19:17:00    Bars               1st

I want to format this into the following

JobName      TotalKilos       TotalBars       StartDate             EndDate               Stage
-----------------------------------------------------------------------------------------------
200          69               6               2012-08-30 13:45:00   2012-08-30 17:23:00   Final
300          60               30              2012-08-30 18:12:00   2012-08-30 19:17:00   1st

This has made me realise I know nothing about SQL. I have tried the following as a start

SELECT JobName, SUM(ProductionCounter)
FROM vwOeeInterval
WHERE (CounterName = 'Kilos')
GROUP BY JobName

SELECT JobName, SUM(ProductionCounter)
FROM vwOeeInterval
WHERE (CounterName = 'Bars')
GROUP BY JobName

My questions are:

  1. How do I combine these into one query?

  2. How would I query for the columns such as StartDate and Stage? Especially where the EndDate is not filled in until the job completes?

Any pointers would be much appreciated.

  • 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-10T17:40:54+00:00Added an answer on June 10, 2026 at 5:40 pm

    Here you go:

    SELECT 
    JobName, 
    SUM(CASE CounterName WHEN 'Kilos' THEN ProductionCounter ELSE 0 END) AS SumKilos,
    SUM(CASE CounterName WHEN 'Bars' THEN ProductionCounter ELSE 0 END) AS SumBars,
    MIN(StartDate), 
    MAX(COALESCE(EndDate, 'undefined date')), 
    MAX(Stage)
    FROM vwOeeInterval
    WHERE CounterName IN ('Kilos', 'Bars')
    GROUP BY JobName;
    

    Not sure if this MAX(COALESCE(EndDate, 'undefined date')) works in SQL Server, since datatype’s might clash. You just need COALESCE(), when you want to replace the NULL value. If it does not work, just do MAX(EndDate) and you’re fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2005 I have a table with data that looks something like
On MS SQL Server 2005, I have a table that I want to replace.
I have a junction table in my SQL Server 2005 database that consist of
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have an ADOQuery that inserts a record to SQL Server 2005 table that
I use SQL Server 2005. I have a simple logging table that my web
enter code here I have a table on SQL server 2005 with bigint primary
i have table with filmname and actors column in sql server 2005 i want
[using SQL Server 2005] I have a table full of users, I want to
I'm using Sql-Server 2005 I have Users table with userID and gender. I want

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.