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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:46:15+00:00 2026-06-15T15:46:15+00:00

I have a table of items which change status every few weeks. I want

  • 0

I have a table of items which change status every few weeks. I want to look at an arbitrary day and figure out how many items were in each status.

For example:

tbl_ItemHistory

ItemID
StatusChangeDate
StatusID

Sample data:

1001, 1/1/2010, 1
1001, 4/5/2010, 2
1001, 6/15/2010, 4
1002, 4/1/2010, 1
1002, 6/1/2010, 3
...

So I need to figure out how many items were in each status for a given day. So on 5/1/2010, there was one item (1001) in status 2 and one item in status 1 (1002).

I want to create a cached table every night that has a row for every item and every day of the year so I can show status changes over time in a chart. I don’t know much about SQL. I was thinking about using a for loop, but based on some of the creative answers I’ve seen on the forum, I doubt that’s the right way.

I’m using SQL Server 2008R2

I looked around and I think this is similar to this question: https://stackoverflow.com/questions/11183164/show-data-change-over-time-in-a-chart but that one wasn’t answered. Is there a way to do these things?

  • 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-15T15:46:17+00:00Added an answer on June 15, 2026 at 3:46 pm

    A coworker showed me a cool way to do it so I thought I would contribute it to the community:

    declare @test table (ItemID int, StatusChangeDate datetime, StatusId tinyint);
    insert @test values
    (1001, '1/1/2010', 1),
    (1001, '4/5/2010', 2),
    (1001, '6/15/2010', 4),
    (1002, '4/2/2010', 1),
    (1002, '6/1/2010', 3);
    with
    itzik1(N) as (
        select 1 union all select 1 union all
        select 1 union all select 1), --4
    itzik2(N) as (select 1 from itzik1 i cross join itzik1), --16
    itzik3(N) as (select 1 from itzik2 i cross join itzik2), --256
    itzik4(N) as (select 1 from itzik3 i cross join itzik3), --65536 (184 years)
    tally(N) as (select row_number() over (order by (select null)) from itzik4)
    select ItemID, StatusChangeDate, StatusId from(
        select
        test.ItemID,
        dates.StatusChangeDate,
        test.StatusId,
        row_number() over (
            partition by test.ItemId, dates.StatusChangeDate
            order by test.StatusChangeDate desc) as rnbr
        from @test test
        join (
            select dateadd(dd, N,
                (select min(StatusChangeDate) from @test) --First possible date
                ) as StatusChangeDate
            from tally) dates
        on test.StatusChangeDate <= dates.StatusChangeDate
        and dates.StatusChangeDate <= getdate()
    ) result
    where rnbr = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table Transactions(store_id, item_id, price). I want to find store_id's which sell
I have a table which has columns of data that contain status. Two example
I have a table Users and a table Items In the Items table, I
I'm working on a very basic shopping cart system. I have a table items
I have a table of items. item has id, score 1, score 2. I
I have a table of items stored this way : A1 | B1 A1
I have a table of items, user should be able to choose some of
I have two tables: Table items: | id | name | description | |
Say I have a table named items : id int(11) ... tag int(11) And
let's say that I have a table called Items (ID int, Done int, Total

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.