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

  • Home
  • SEARCH
  • 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 7411595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:24:37+00:00 2026-05-29T06:24:37+00:00

Given a table of structure: CREATE TABLE [dbo].[Example]( [FiscalYear] INT NOT NULL, [Hours] [decimal](28,

  • 0

Given a table of structure:

CREATE TABLE [dbo].[Example](  
    [FiscalYear] INT NOT NULL,  
    [Hours] [decimal](28, 2) NULL,  
    CONSTRAINT [PK_Example] PRIMARY KEY CLUSTERED (
        [FiscalYear] ASC  
    )
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
        ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

with data

INSERT INTO [Example] ([FiscalYear], [Hours]) VALUES (2009 ,25) GO 
INSERT INTO [Example] ([FiscalYear], [Hours]) VALUES (2010 ,20) GO
INSERT INTO [Example] ([FiscalYear], [Hours]) VALUES (2011 ,30)

Assuming we provide a date range in periods (i.e. March 2010 is expressed as 201003) for instance @StartPeriod = 201009, @EndPeriod = 201108

How would I query table Example so that I get representative data for the periods covered in the date range.

In the case with the date range given above, we would get:

(.33333*25) + (.66666*30) = 26.66666

Where .33333 is because 2010 has only 4 months (1/3 of the total year) represented in the period date range, and .66666 is because 2011 has 8 months (2/3 of the total year) represented in the period date range.

How do I write a query to get those representative “weights”? Also assume that in some cases a date range could span multiple years i.e. @StartPeriod = 200905, @EndPeriod = 201104 in which case 2010 would be represented by 100% (1).

  • 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-05-29T06:24:38+00:00Added an answer on May 29, 2026 at 6:24 am

    You can build a list of all months in a CTE. Matching months contribute 1/12th of the yearly number of hours. You can use an inner join to add up only the matching months:

    ; with  AllMonths as
            (
            select  cast('2001-01-01' as datetime) as MonthStart
            union all
            select  dateadd(month, 1, MonthStart)
            from    AllMonths
            where   MonthStart < '2020-01-01'
            )
    select  sum(e.Hours/12)
    from    AllMonths am
    join    @example e
    on      e.FiscalYear = datepart(year, am.MonthStart)
    where   am.MonthStart between '2010-09-01' and '2011-08-01'
    option  (maxrecursion 0)
    

    The result is 4/12*20+8/12*30 = 26.66. Your example answer was 21.66 because it uses 20 as the number of hours for 2011. THis answer is using 30 per the dataset.

    Working example at SE Data.

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

Sidebar

Related Questions

Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given a table such as: CREATE TABLE dbo.MyTestData (testdata varchar(50) NOT NULL) ALTER TABLE
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)
Sql Server 2005 Table Structure CREATE TABLE [dbo].[Rate]( [RateID] [bigint] IDENTITY(1,1) NOT NULL, [PairID]
Given the following table structure: CREATE TABLE user ( uid INT(11) auto_increment, name VARCHAR(200),
I have a organization name table with the following structure given below: CREATE TABLE
I have a table similar to this: CREATE TABLE example ( id integer primary
Table Structure is: create table fruit ( id int identity(1,1), name varchar(max) ) create
I am trying to create an entity for the table structure given belos (id,

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.