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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:19:36+00:00 2026-05-27T07:19:36+00:00

I have a table that lists month totals (targets) person total month ———– ———————

  • 0

I have a table that lists month totals (targets)

person      total                 month       
----------- --------------------- ----------- 
1001        114.00                201005      
1001        120.00                201006      
1001        120.00                201007      
1001        120.00                201008      
.
1002        114.00                201005      
1002        222.00                201006      
1002        333.00                201007      
1002        111.00                201008      
.
.

but month is an integer(!)

I also have another table that has a list of working days (calendar)

tran_date               day_type
----------------------- ---------------------------------
1999-05-01 00:00:00.000 WEEKEND
1999-05-02 00:00:00.000 WEEKEND
1999-05-03 00:00:00.000 WORKING_DAY
1999-05-04 00:00:00.000 WORKING_DAY

1999-06-01 00:00:00.000 .....
.
.
.

What I want to do is get a list of dates with the average for that day based on the number of days in the month where day_type is ‘WORKING_DAY’ / the month’s total.

so if I had say 20 working days in 201005 then I’d get an average of 114/20 on each working day, while the other days would be 0.

somthing like

person   tran_date               day_avg
-------  ----------------------- ---------------------------------
1001     2010-05-01 00:00:00.000 0
1001     2010-05-02 00:00:00.000 0
1001     2010-05-03 00:00:00.000 114/2 (as there are two working days)
1001     2010-05-04 00:00:00.000 114/2 (as there are two working days)
.
.
.

It has to be done as a CTE as this is a limitation of the target system (I can only do one statement)
I can start off with (Dates to

WITH 
Dates AS
(
    SELECT CAST('19990501' as datetime) TRAN_DATE
    UNION ALL
    SELECT TRAN_DATE + 1
    FROM Dates
    WHERE TRAN_DATE + 1 <= CAST('20120430' as datetime)
),
Targets as
(
   select CAST(cast(month as nvarchar) + '01' as dateTime) mon_start, 
            DATEADD(MONTH, 1, CAST(cast(month as nvarchar) + '01' as dateTime)) mon_end, 
             total
   from targets
)
select ????
  • 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-27T07:19:37+00:00Added an answer on May 27, 2026 at 7:19 am

    Sample data (may vary):

    select * into #totals from (
    select '1001' as person, 114.00  as total, 199905 as month union
    select '1001', 120.00, 199906 union
    select '1001', 120.00, 199907 union
    select '1001', 120.00, 199908  
    
    ) t
    
    select * into #calendar from (
    select cast('19990501' as datetime) as tran_date, 'WEEKEND' as day_type union
    select '19990502', 'WEEKEND' union
    select '19990503', 'WORKING_DAY' union
    select '19990504', 'WORKING_DAY' union
    select '19990505', 'WORKING_DAY' union
    select '19990601', 'WEEKEND' union
    select '19990602', 'WORKING_DAY' union
    select '19990603', 'WORKING_DAY' union
    select '19990604', 'WORKING_DAY' union
    select '19990605', 'WORKING_DAY' union
    select '19990606', 'WORKING_DAY' union
    select '19990701', 'WORKING_DAY' union
    select '19990702', 'WEEKEND' union
    select '19990703', 'WEEKEND' union
    select '19990704', 'WORKING_DAY' union
    select '19990801', 'WORKING_DAY' union
    select '19990802', 'WORKING_DAY' union
    select '19990803', 'WEEKEND' union
    select '19990804', 'WEEKEND' union
    select '19990805', 'WORKING_DAY' union
    select '19990901', 'WORKING_DAY'
    ) t
    

    Select statement, it returns 0 if the day is ‘weekend’ or not exists in calendar table. Please keep in mind that MAXRECURSION is a value between 0 and 32,767.

    ;with dates as ( 
        select cast('19990501' as datetime) as tran_date 
        union all 
        select dateadd(dd, 1, tran_date) 
        from dates where dateadd(dd, 1, tran_date) <= cast('20010101' as datetime) 
    ) 
    select t.person , d.tran_date, (case when wd.tran_date is not null then t.total / w_days else 0 end) as day_avg 
    from dates d 
    left join #totals t on  
        datepart(yy, d.tran_date) * 100 + datepart(mm, d.tran_date) = t.month 
    left join ( 
            select datepart(yy, tran_date) * 100 + datepart(mm, tran_date) as month, count(*) as w_days 
            from #calendar 
            where day_type = 'WORKING_DAY' 
            group by datepart(yy, tran_date) * 100 + datepart(mm, tran_date) 
    ) c on t.month = c.month  
    left join #calendar wd on d.tran_date = wd.tran_date and wd.day_type = 'WORKING_DAY' 
    where t.person is not null
    option(maxrecursion 20000) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table that lists all awarded cups: Name: Cups Columns: Month,
I have a table that lists number of comments from a particular site like
I have a PHP calendar that lists all of the days of the month
I have a table that has about 1/2 million records in it. Each month
Here is my situation: I have one table that contains a list of drugs
I have a table that contains multiple dropdown menus for a list of profile
I have a DB table that contains a comma separated list of ID's (ints)
We have a table value function that returns a list of people you may
I have a mysql table (table-A) that contains a list of unique product ids.
I have a table articles that has a column company which has list of

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.