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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:43+00:00 2026-05-27T15:38:43+00:00

how to get previous record depend on unique_id and date? i want make my

  • 0

how to get previous record depend on unique_id and date?
i want make my table (dbo : Daily_Summary) :

ID    Partner     part_no    Date      Periode_Sum     Previous_Sum
1      aa            12    2011-12-21       40             
2      aa            12    2011-12-22       30              40
3      bb2           13    2011-12-22       20             
4      bb2           13    2011-12-23       30              20
5                          2011-12-24
6                          2011-12-25
7      aa            12    2011-12-26       30              70
8      bb2           13    2011-12-27       40              50
and so on

which ‘Previous_Sum’ is function update, and the function only update the previous record of Periode_Sum. And group by partner, part_no and date

My display query had error to.
When i display the previous record by partner, part_no and date. the record of previous_sum only displayed the first partner.
here is my query :

 SELECT ds.partner_id, ds.part_no, ds. periode_in
        , ds.periode_out, ds.periode_date, ds.periode_sum, 
(   
  SELECT F1.periode_sum
  FROM Daily_Summary as F1 where 
  F1.periode_sum =
    ( 
      SELECT  Max(F2.periode_sum) 
      FROM Daily_Summary as F2 where F2.periode_date < ds.periode_date
      and F2.partner_id=ds.partner_id and F2.part_no = ds.part_no
    ) 
) AS Prev_Value
FROM Daily_Summary ds
where stsrc='A'
order by ds.partner_id, ds.periode_date

i tried using declare new param but not working :

DECLARE @prev_sum INT = 0
DECLARE @dudut INT = 2

SELECT @prev_sum = 
(select sum(periode_sum) 
    from Daily_Summary t1 
    where t1.partner_id = ds.partner_id and t1.part_no = ds.part_no
        and t1.periode_date < ds.periode_date --and t1.id < t.id
) 
FROM Daily_Summary ds
where stsrc='A'
order by ds.partner_id, ds.periode_date 

select partner_id,part_no,model,periode_sum,
case when @prev_sum is null then @dudut else @prev_sum end 
FROM daily_summary
where stsrc='A'
  • 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-27T15:38:43+00:00Added an answer on May 27, 2026 at 3:38 pm
    select * into #tab from (
        select 1 as id, 'aa' as partner, 12 as part_no, 
            cast('2011-12-21' as datetime) as date, 40 as periode_sum union all
        select 2, 'aa', 12, '2011-12-22', 30 union all
        select 3, 'bb2', 13, '2011-12-22', 20 union all
        select 4, 'bb2', 13, '2011-12-23', 30 union all
        select 5, null, null, '2011-12-24', null union all
        select 6, null, null, '2011-12-25', null union all
        select 7, 'aa', 12, '2011-12-26', 30 union all
        select 8, 'bb2', 13, '2011-12-27', 40
    ) t
    
    
    select *, (select sum(periode_sum) 
        from #tab t1 
        where t1.partner = t.partner and t1.part_no = t.part_no
            and t1.date < t.date and t1.id < t.id
    ) as previous_sum
    from #tab t
    

    If more than one row per day for specific pair (partner, part_no) is allowed then instead of and t1.date < t.date and t1.id < t.id you should use and t1.date <= t.date and t1.id < t.id. You can use just and t1.id < t.id if id ensures proper order (in time) for all rows.

    Result:

    id   partner    part_no    date                       periode_sum     previous_sum
    1    aa         12         2011-12-21 00:00:00.000    40              NULL
    2    aa         12         2011-12-22 00:00:00.000    30              40
    3    bb2        13         2011-12-22 00:00:00.000    20              NULL
    4    bb2        13         2011-12-23 00:00:00.000    30              20
    5    NULL       NULL       2011-12-24 00:00:00.000    NULL            NULL
    6    NULL       NULL       2011-12-25 00:00:00.000    NULL            NULL
    7    aa         12         2011-12-26 00:00:00.000    30              70
    8    bb2        13         2011-12-27 00:00:00.000    40              50
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an exchange rate table. I need to get current rate and previous
In MySQL you can get the next or previous record based on the key/id
Using MS Access database I want to display today record min (time) and previous
How to get previous page URL using jQuery? I am using the following code
How can I get the previous version of data of a Row in a
I managed to get some help from a previous question and I have the
I'm trying to get some code working that a previous developer has written. Yep,
I'm working through previous years ACM Programming Competition problems trying to get better at
if i click on any checkbox all previous checkboxes must get checked my logic
In the previous versions of Virtual PC, the Host's CD/DVD drives just get assigned

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.