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

The Archive Base Latest Questions

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

I need help writing a T-SQL query that will generate 52 rows of data

  • 0

I need help writing a T-SQL query that will generate 52 rows of data per franchise from a table that will often contain gaps in the 52 week sequence per franchise (i.e., the franchise may have reported data bi-weekly or has not been in business for a full year).

The table I’m querying against looks something like this:

FranchiseId | Date | ContractHours | PrivateHours

and I need to join it to a table similar to this:

FranchiseId | Name

The output of the query needs to look like this:

Name | Date       | ContractHours | PrivateHours
----   ----------   -------------   ------------
AZ1    08-02-2011             292            897
AZ1    07-26-2011               0              0 -- default to 0's for gaps in sequence
...
AZ1    08-03-2010              45            125 -- row 52 for AZ1
AZ2    08-02-2011             382            239
...
AZ2    07-26-2011               0              0 -- row 52 for AZ2

I need this style of output for every franchise, i.e., 52 rows of data with default rows for any gaps in the 52 week sequence, in a single result set. Thus, if there are 100 franchises, the result set should be 5200 rows.

What I’ve Tried

I’ve tried the typical suggestions of:

  • Create a table with all possible dates
  • LEFT OUTER JOIN this to the table of data needed

The problems I’m running into are

  1. ensuring that for every franchise their are 52 rows and
  2. filling in gaps with the franchise name and 0 for hours, I can’t
    have the following in the result set:

    Name | Date       | ContractHours | PrivateHours
    ----   ----------   -------------   ------------
    NULL   08-02-2011            NULL           NULL
    

I don’t know where to go from here? Is there an efficient way to write a T-SQL query that will produce the required output?

  • 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-24T12:13:30+00:00Added an answer on May 24, 2026 at 12:13 pm

    The bare bones is this

    • Generate 52 week ranges
    • Cross join with Franchise
    • LEFT JOIN the actual date
    • ISNULL to substitute zeroes

    So, like this, untested

    ;WITH cDATE AS
    (
        SELECT
            CAST('20100101' AS date /*smalldatetime*/) AS StartOfWeek, 
            CAST('20100101' AS date /*smalldatetime*/) + 6 AS EndOfWeek
        UNION ALL
        SELECT StartOfWeek + 7, EndOfWeek + 7
        FROM cDATE WHERE StartOfWeek + 7 < '20110101'
    ), Possibles AS
    (
        SELECT
            StartOfWeek, FranchiseID
        FROM
            cDATE CROSS JOIN Franchise
    )
    SELECT
        P.FranchiseID,
        P.StartOfWeek, 
        ISNULL(SUM(O.ContractHours), 0), 
        ISNULL(SUM(O.PrivateHours), 0)
    FROM
        Possibles P
        LEFT JOIN
        TheOtherTable O ON P.FranchiseID = O.FranchiseID AND 
                        O.Date BETWEEN P.StartOfWeek AND P.EndOfWeek
    GROUP BY
        P.FranchiseID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with writing some code that will create a random number from
I need a little help writing a query. I have this data... vDir iNumber
I need help writing select statement that will do an average of the most
I could use some help writing a SQL query. I'm trying to display some
i need help with disk_total_space function.. i have this on my code <?php $sql=select
I really need help with interfaces in general... Any resources that you guys would
I have a regex call that I need help with. I haven't posted my
I am newbie to db programming and need help with optimizing this query: Given
Writing my first SQL query to run specifically as a SQL Job and I'm
Okay, I need help. I'm usually pretty good at SQL queries but this one

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.