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

The Archive Base Latest Questions

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

I have table in SQL Server DB: ID Open_date Close_date 1 01-01-2010 01-03-2010 2

  • 0

I have table in SQL Server DB:

ID    Open_date     Close_date
1     01-01-2010    01-03-2010
2     21-01-2011    12-02-2011
3     01-03-2010    NULL
4     10-01-2010    NULL

I need to make some T-SQL query that will return:

Month    Year    Open    Close
01       2010    2       0
02       2010    0       0
03       2010    0       1
04       2010    0       0
05       2010    0       0
06       2010    0       0
07       2010    0       0
08       2010    0       0
09       2010    0       0
10       2010    0       0
11       2010    0       0
12       2010    0       0
01       2011    1       0
02       2011    0       1

Amount of entries in result set equals amount of months between min value from both ‘Open_date’ and ‘Close_date’ columns of table from DB and max value from same columns. The problem is how to find min and max value from two date columns, then generate dates from min to max and then use this temporary table(or what it would be) to count number of open and closed entries for every date from temporary table.

  • 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:33:04+00:00Added an answer on May 27, 2026 at 7:33 am

    I changed the dates a bit, since I’m using a different date system, but this should work. Accept if it does, please!

    DECLARE @TABLE1 TABLE
    (
        ID INT
        , Open_date DATETIME
        , Close_date DATETIME
    )
    
    INSERT INTO @TABLE1 (ID, Open_date, Close_date)
          SELECT 1, '01-01-2010', '03-01-2010'
    UNION SELECT 2, '01-21-2011', '02-12-2011'
    UNION SELECT 3, '03-01-2010', NULL
    UNION SELECT 4, '01-10-2010', NULL
    
    
    DECLARE @MIN_DATE DATETIME
    DECLARE @MAX_DATE DATETIME
    
    SELECT @MIN_DATE = MIN(d)
        , @MAX_DATE = MAX(d)
    FROM
    (
        SELECT Open_date AS d
        FROM @TABLE1
    
            UNION
    
        SELECT Close_date AS d
        FROM @TABLE1
    )a
    
    --SELECT @MIN_DATE, @MAX_DATE
    
    
    
    DECLARE @DATES TABLE
    (
        ID INT IDENTITY(1,1)
        , [date] DATETIME
    )
    
    DECLARE @DATE DATETIME
    SET @DATE = @MIN_DATE
    
    WHILE (@DATE <= @MAX_DATE)
    BEGIN
    
        INSERT INTO @DATES ([date])
        VALUES (@DATE)
    
        SET @DATE = @DATE + 1
    
    END
    
    --SELECT *
    --FROM @DATES
    
    SELECT MONTH(a.[date]) AS [Month]
        , YEAR(a.[date]) AS [Year]
        , SUM(CASE WHEN b.Open_date IS NOT NULL THEN 1 ELSE 0 END) AS [Open]
        , SUM(CASE WHEN b.Close_date IS NOT NULL THEN 1 ELSE 0 END) AS [Close]
    FROM @DATES a
    LEFT JOIN @TABLE1 b
        ON a.[date] = b.Open_date
    GROUP BY YEAR(a.[date]), MONTH(a.[date])
    ORDER BY YEAR(a.[date]), MONTH(a.[date])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an interesting table query (SQL Server 2008) that fails with a different
I have a table in SQL server that has the normal tree structure of
I have a table on SQL Server 2005 that was about 4gb in size.
I have a table in a SQL Server 2005 database with a trigger that
I have a SQL Server table that contains users & their grades. For simplicity's
I am using SQL Server 2008. I have a table (TBL_FILE) that stores user
i have created that table on my sql server : create table Empolyee( ESSN
I have a table named t_Student in Microsoft SQL Server 2005 database. In that
I have a table in SQL Server 2005 which has three columns: id (int),
I have a table in SQL Server 2000 Standard Edition called dbo.T668 (don't blame

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.