2011-12-23 41 views
2

如何獲取以前的記錄取決於unique_id和日期? 我想使我的表(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 

這「Previous_Sum」是功能更新和功能僅更新Periode_Sum的紀錄。並按夥伴分組,part_no和date

我的顯示查詢有錯誤。 當我顯示由夥伴,part_no和日期的前一個記錄。 previous_sum的記錄只顯示第一個夥伴。 這裏是我的查詢:

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 

我嘗試使用申報新PARAM但不工作:

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' 
+0

是哪一個? MySQL或SQL服務器? – Johan 2011-12-23 08:11:48

+0

使用SQL服務器 – tyo 2011-12-23 08:23:09

+0

是「Previous_Sum」是由合作伙伴,part_no和date分組的「Periode_Sum」也許增加更多行樣本數據... – gbn 2011-12-23 08:32:37

回答

1
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 

如果每天超過一排用於對特定的(partnerpart_no)被允許那麼應該使用and t1.date <= t.date and t1.id < t.id而不是and t1.date < t.date and t1.id < t.id。如果id確保所有行的正確順序(及時),則只能使用and t1.id < t.id

結果:

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

謝謝michal。但我的問題是如何創建具有該條件的函數更新爲previous_sum,其中previous_sum是daily_summary表的列。 我試圖聲明新的參數,但我無法加載函數。 – tyo 2011-12-23 09:51:19

+0

@tyo,我不清楚。您是否想創建一個函數,該函數返回特定行的'previous_sum'值或爲所有行更新'previous_sum'列中的值的存儲過程?你能否顯示你提到的函數和調用的上下文(添加到你的問題)。請添加預期結果(按行)。 – 2011-12-23 10:03:51

+0

yap創建存儲過程就像你的意思。 我已經創建了查詢,但所有的值都是空的 – tyo 2011-12-27 02:55:35

相關問題