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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:48:05+00:00 2026-06-12T21:48:05+00:00

I am working in SQL Server 2008. I am writing a stored procedure which

  • 0

I am working in SQL Server 2008. I am writing a stored procedure which aggregates data on a weekly basis.

Code for aggregation is

ALTER PROCEDURE [dbo].[ups_URLBatchStats] 
   (@startDate datetime, @endDate datetime,@source varchar(8))
AS BEGIN
    SELECT 
       DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6) AS [Week Commencing], 
       SUM(Unresolved) AS Unresolved,
       SUM(Resolved) AS Resolved,
       SUM(TurkSpend) AS TurkSpend
    FROM 
       dbo.V_URLBatchStats 
    WHERE
       ScheduleDate BETWEEN @startDate AND @endDate 
       AND Source = ISNULL(@source, Source)
    GROUP BY
       DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6)
    ORDER BY 
       DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6)
END

Show it gives me correct result, but if there are no data for any week I have to show it with values of ‘0’ in the columns.

So how can I handled these skipped weekly rows?

Thanks in advance.

  • 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-12T21:48:06+00:00Added an answer on June 12, 2026 at 9:48 pm

    It seems like you are currently selecting the wrong week, I tried to fix this:

    SELECT dateadd(wk, number, DATEADD(wk, DATEDIFF(wk, 0, @startdate)-1, 6)) AS [Week Commencing], 
    coalesce(SUM(Unresolved), 0) AS Unresolved,
    coalesce(sum(Resolved), 0)as Resolved,
    coalesce(sum(TurkSpend), 0) as TurkSpend
    FROM dbo.V_URLBatchStats 
    right join
    master..spt_values 
    on ScheduleDate between @startDate and @endDate and Source=ISNULL(@source,Source)
    where type = 'P' and dateadd(wk, number, DATEADD(wk, DATEDIFF(wk, 0, @startdate)-1, 6)) <= @enddate
    GROUP BY dateadd(wk, number, DATEADD(wk, DATEDIFF(wk, 0, @startdate)-1, 6))
    ORDER BY dateadd(wk, number, DATEADD(wk, DATEDIFF(wk, 0, @startdate)-1, 6))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with SQL Server 2008 R2. I have a table of hierarchical data, with
This isn't working in SQL Server 2008: ALTER TABLE Employee ALTER COLUMN CityBorn SET
I'm working with stored procedures in SQL Server 2008 and I've come to learn
So I'm working on SQL Server 2008 and I have this query which should
I am working on SQL SERVER 2008 & 2008 R2. How can I rename
I'm working on SQL SERVER 2008 R2 Performance tunning part but I'm new in
I am trying to put full text search working on SQL Server 2008, however
I'm working on a SQL Server 2008 database. This database has two tables. Book
I am working on a sql server 2008 DB and asp.net mvc web E-commerce
Possible Duplicate: SQL Server 2008 Spatial: find a point in polygon I am working

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.