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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:12:13+00:00 2026-06-06T00:12:13+00:00

I need to generate report that will show number of new / changed rows

  • 0

I need to generate report that will show number of new / changed rows on per-day basis for SCD table.

Here is the SQL to create the table:

create table #scd(
  code      nvarchar not null
, startdate date     not null
, enddate   date
);
alter table #scd add constraint pk_scd primary key (code, startdate);


insert into #scd values
 ('A', '2012-06-01', '2012-06-02')
,('B', '2012-06-01', '2012-06-02')
,('A', '2012-06-02', '2012-06-03')
,('B', '2012-06-02', '2012-06-04')
,('A', '2012-06-03', '2012-06-04')
,('A', '2012-06-04', null)
,('B', '2012-06-04', null)
,('C', '2012-06-04', null)

select * from #scd

The result look like this:

code    startdate   enddate
A   2012-06-01  2012-06-02
B   2012-06-01  2012-06-02
A   2012-06-02  2012-06-03
B   2012-06-02  2012-06-04
A   2012-06-03  2012-06-04
A   2012-06-04  NULL
B   2012-06-04  NULL
C   2012-06-04  NULL

Now, I need to produce someting like this:

date            new changed
2012-06-01      2   0
2012-06-02      0   2
2012-06-03      0   1
2012-06-04      1   2

Any help is much appreciated.

  • 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-06T00:12:14+00:00Added an answer on June 6, 2026 at 12:12 am
    ; with
    q_00 as ( -- get new records
        select
              code 
            , startdate
        from #scd as s
        where s.startdate = (select MIN(xx.startdate) from #scd as xx where xx.code = s.code)
    ),
    q_01 as ( -- get changed records, those that are not new
        select
              s.code 
            , s.startdate
        from #scd      as s
        left join q_00 as b on b.code = s.code and b.startdate = s.startdate
        where b.code is null
    ),
    q_03 as ( -- get the list of all possible dates
        select distinct 
            startdate
        from #scd
    ),
    q_04 as ( -- count new records per date
        select
              startdate
            , COUNT(1)  as new_rec
        from q_00
        group by startdate
    ),
    q_05 as ( -- count changed records per date
        select
              startdate
            , COUNT(1)  as chn_rec
        from q_01
        group by startdate
    )
    select
          a.startdate          as OnDate
        , coalesce(new_rec, 0) as new_records
        , coalesce(chn_rec, 0) as changed_records
    from      q_03 as a
    left join q_04 as b on b.startdate = a.startdate
    left join q_05 as c on c.startdate = a.startdate
    order by  a.startdate
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a report using SSRS 2008 that will show the top
i will need to generate a report based on user input for every food
I need to create a simple application that would generate reports based on database
I would like to achieve the following, I need to generate report for multiple
I need to generate a report where the user can choose All Issues, Open
I have a script that will generate a CSV file. The purpose of the
I have a requirement where I need to generate a report about current software
I have a form which will generate a report for mailing. The form opens
I'm having trouble getting the SQL for a report I need to generate. I've
Hello I'm building an application with Yii that will now generate reports. My client

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.