如何获取以前的记录取决于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'
是哪一个? MySQL或SQL服务器? – Johan 2011-12-23 08:11:48
使用SQL服务器 – tyo 2011-12-23 08:23:09
是“Previous_Sum”是由合作伙伴,part_no和date分组的“Periode_Sum”也许增加更多行样本数据... – gbn 2011-12-23 08:32:37