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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:22:29+00:00 2026-06-02T02:22:29+00:00

Slightly difficult to explain and my SQL Server is not the best but anything

  • 0

Slightly difficult to explain and my SQL Server is not the best but anything will do here.

First, create some tables:

CREATE TABLE [dbo].[Quarterly](
[QuarterDate] [datetime] NOT NULL,
[SomeText] [nvarchar](50) NULL,
CONSTRAINT [PK_Quarterly] PRIMARY KEY CLUSTERED 
(
[QuarterDate] ASC
)
GO

CREATE TABLE [dbo].[TmpDegreeDays](
[Date] [datetime] NOT NULL,
[Value] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_TmpDegreeDays] PRIMARY KEY CLUSTERED 
(
[Date] ASC
)
GO

Then insert some data:

INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009CF100000000 AS DateTime), N'Blah')
INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009D4B00000000 AS DateTime), N'Fools')
INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009DA600000000 AS DateTime), N'Later')
INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009E0400000000 AS DateTime), N'Something')
INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009E5E00000000 AS DateTime), N'New year')
INSERT [dbo].[Quarterly] ([QuarterDate], [SomeText]) VALUES (CAST(0x00009EC300000000 AS DateTime), N'In april')

Then insert date ranges from 2010-01-01 to (including) 2012-03-10 into table TmpDegreeDays

Finally:

I want to calculate the SUM of [Value] in TmpDegreeDays for each record in the Quarterly table between the current QuarterDate and the next record in the Quarterly resultset.

Something like:

DECLARE @startDate datetime, @endDate datetime
SET @startDate = '2010-01-01'
SET @endDate = '2010-12-31'

SELECT q.QuarterDate, q.SomeText, CustomSum = 
(SELECT SUM(CAST([Value] AS float))
FROM TmpDegreeDays 
WHERE [date] >= q.QuarterDate AND *Current QuarterDate* < *Some query here to get next row QuarterDate*)

FROM Quarterly q

WHERE q.QuarterDate BETWEEN @startDate AND @endDate

Example of final output I am looking for:

2010-01-01   Sum of [Value] between 2010-01-01 and 2010-03-31
2010-04-01   Sum of [Value] between 2010-04-01 and 2010-06-30
2010-07-01   Sum of [Value] between 2010-07-01 and 2010-09-31
2010-10-03   Sum of [Value] between 2010-10-03 and 2010-10-03

Does this make sense?

  • 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-02T02:22:31+00:00Added an answer on June 2, 2026 at 2:22 am

    You could try something like this – using a CTE (available in SQL Server 2005 and newer) to find the dates for each quarter and then summing up the degree values:

    ;WITH QuarterlyDates AS
    (        
       SELECT 
           QuarterDate, 
           QuarterEndDate = DATEADD(MILLISECOND, -3, DATEADD(MONTH, 3, QuarterDate))
       FROM [dbo].[Quarterly]
    )
    SELECT
        qd.QuarterDate, qd.QuarterEndDate,
        DegreeSum = (SELECT SUM(DegreeValue) 
                     FROM [dbo].[TmpDegreeDays] 
                     WHERE [Date] BETWEEN qd.QuarterDate AND qd.QuarterEndDate)
    FROM
        QuarterlyDates qd
    

    The strange function to determine the QuarterEndDate is rooted in the fact that the DATETIME has a precision of 3.33ms in SQL Server. Thus, the last date for a given month is the last day of that month, and the time is 23:59:59.997 (not .999). Therefore, I need to add three months to the quarter’s start date, and then subtract 3 milliseconds from that start date of the next quarter to get the last millisecond of this quarter in question.

    Update: OK, in order to get the start and end date for a quarter from the table, you need two nested CTE’s in order to determine the “end date of the next quarter minus 3 milliseconds” for the end of the quarter – something like this:

    ;WITH QuarterStarts AS
    (
        SELECT  
            QuarterDate,
            QNumber = ROW_NUMBER() OVER(ORDER BY QuarterDate)   -- ordering number
        FROM [dbo].[Quarterly]
    ),
    Quarters AS
    (
        SELECT
            QuarterStartDate = q1.QuarterDate,
            QuarterEndDate = DATEADD(MILLISECOND, -3, q2.QuarterDate) 
        FROM
            QuarterStarts q1
        INNER JOIN 
            QuarterStarts q2 ON q2.QNumber = q1.QNumber + 1 
    )
    SELECT
        q.QuarterDate, q.QuarterEndDate,
        DegreeSum = (SELECT SUM(DegreeValue) 
                     FROM [dbo].[TmpDegreeDays] 
                     WHERE [Date] BETWEEN q.QuarterDate AND q.QuarterEndDate)
    FROM
        Quarters q
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title may be slightly ambiguous or not clear, I'll first explain what
Normally people say MFC is little clumsy. It makes UI development slightly difficult to
Slightly daft, but... Is there a way to prevent Visual Studio treating a .jpg
This is a slightly tricky question. I am using NSDateFormatter on the iPhone but
I've got a slightly large sql script saved as a textfile. It totals in
We have a SQL Server table containing Company Name, Address, and Contact name (among
Am facing a problem that may be slightly complicated to explain and understand as
I have 2 databases with the same data, but slightly different data types in
I am looking at this Gist (trying my hand at slightly more difficult Node
Question slightly in the abstract... We have a situation where we have a struct

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.