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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:09:45+00:00 2026-05-26T12:09:45+00:00

My table records is like below ym cnt 200901 57 200902 62 200903 67

  • 0

My table records is like below

ym  cnt
200901  57
200902  62
200903  67
...
201001  84
201002  75
201003  75
...
201101  79
201102  77
201103  80
...

I want to computer the diff between current month and per month .
the result would like below …

ym  cnt       diff
200901  57        57   
200902  62        5  (62 - 57)
200903  67        5  (67 - 62) 
...
201001  84       ...
201002  75
201003  75
...
201101  79
201102  77
201103  80
...

Can anyone told me how to wrote a sql to got the result and with a good performance ?

UPDATE:

sorry for simple words

my solution is

step1: input the currentmonth data into temp table1

step2: input the permonth data into temp table2

step3: left join 2 tables to compute the result 

Temp_Table1

SELECT  (ym - 1) as ym , COUNT( item_cnt ) as cnt
FROM _table 
GROUP BY (ym - 1 ) 
order by ym

Temp_Table2

SELECT  ym ,  COUNT( item_cnt ) as cnt
FROM _table
GROUP BY ym 
order by ym



select ym , (b.cnt - a.cnt) as diff from Temp_Table2 a 
           left join Temp_Table1 b
                on a.ym = b.ym

*If i want to compare the diff between the month in this year and last year
I can only change the ym – 1 to ym – 100
*

but , actually , the group by key is not only ym

there is max 15 keys and max 100 millions records

so , I wonder a good solution can easy to manager the source

and good performance.

  • 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-26T12:09:45+00:00Added an answer on May 26, 2026 at 12:09 pm

    For MSSQL, this has one reference to the table, so potentially it can be faster (maybe not) than left join which has two references to the table:

    -- ================
    -- sample data
    -- ================
    declare @t table
    (
        ym varchar(6),
        cnt int
    )
    
    insert into @t values ('200901', 57)
    insert into @t values ('200902', 62)
    insert into @t values ('200903', 67)
    insert into @t values ('201001', 84)
    insert into @t values ('201002', 75)
    insert into @t values ('201003', 75)
    
    -- ===========================
    -- solution
    -- ===========================
    select
        ym2,
        diff = case when cnt1 is null then cnt2
            when cnt2 is null then cnt1
            else cnt2 - cnt1
            end
    from
    (
        select
            ym1 = max(case when k = 2 then ym end),
            cnt1 = max(case when k = 2 then cnt end),
            ym2 = max(case when k = 1 then ym end),
            cnt2 = max(case when k = 1 then cnt end)
        from
        (
            select 
                *, 
                rn = row_number() over(order by ym)
            from @t
        ) t1
        cross join
        (
            select k = 1 union all select k = 2
        ) t2
        group by rn + k
    ) t
    where ym2 is not null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data table like below. Say I have thousands of records: +----------------------+
I want to fetch Hierarchical/Tree data something like below from a Table which has
I have a table of records like below int_record_id int_category_id str_name int_order bit_active 1
I am having a table with records like this in MS Access: ID field2
Hi experts: I have a table Called tblAlarm and it has some records like
My table has records for every minute, now I want to fetch records with
My table has records like these 23-MAY-11 11.40.39.000000 AM The following query brings nothing
My table contains some rows like below Lcd - projector A & A products
I have a exchange rate table like below FromCurrency ToCurrency ValidFrom ExchangeRate USD ZAR
Hi mine css class is as below for table records table.tblBaseTblData{ margin:0px; padding:0px; *margin-left:10px;

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.