Need some help writing a SQL Query

Posted on 2011-10-30
Last Modified: 2012-05-12
So i am working on a query for summarizing machine delay instances.

What i have now (code attached) shows a query that summarizes delay by Maincategory for a set time period. In the example its 8 hours.

What i would like to do is add another column that shows the cumulative for the week. I would guess i need to run another select statement for another time range and somehow union the tables together.

Any advice would be greatly appreciated. Thanks
declare @Starttime datetime
declare @Endtime datetime
declare @PrevHours int

--set variables here
set @EndTime = '2011-10-26 23:00:00'
set @PrevHours = -8
set @StartTime = dateadd(hh,@PrevHours,@EndTime)

SELECT ISNULL(MainCategoryDescription,'Not Classified') as 'Delay Type', round(sum(DelayHours),2) as Hours
FROM [RKBBackEnd].[dbo].[v_Delay]
where timedelaybegin > @starttime and timedelayend < @Endtime and LineID = 1
group by MainCategoryDescription order by Hours desc

Open in new window

Question by:michael_krell
    LVL 50

    Accepted Solution

    what are you trying to accumulated for the week?

    declare @Starttime datetime
    ,@Endtime datetime
    , @PrevHours int
    ,@Startweek dateweek

    --set variables here
    select @EndTime = '2011-10-26 23:00:00'
    , @PrevHours = -8
    , @StartTime = dateadd(hh,@PrevHours,@EndTime)

    ;with cte as (select
    coalesce(MainCategoryDescription,'Not Classified') as [Delay Type]
         , DelayHours
    FROM [RKBBackEnd].[dbo].[v_Delay]
    where timedelaybegin > @startweek
      and timedelayend < @Endtime
      and LineID = 1

    SELECT [Delay Type]
           , round(sum(case when timedelaybegin > @starttime then DelayHours end),2) as Hours
           , count(*) as [delays lastweek]
           , round(sum(DelayHours),2) as [delay Hours last week]
    FROM cte

    group by [delay type]
    order by 1,2 desc

    Author Comment

    what you gave me seems to do the trick. Nice!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now