分店 開始日(租約) 結束日(租約) 開始日(租期) 結束日(租期) Rental % Rental
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 1200
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3%
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 1%
100 2000-1-1 2011-04-31 2000-6-1 2000-12-30 5000
100 2000-1-1 2011-04-31 2001-1-1 2008-1-31 3500
100 2000-1-1 2011-04-31 2008-2-1 2011-04-31 2000
101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 1000
101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 0 5%
表結構如上,希望能做到租期相同(開始與結束時間相同的時候)的話那麼就把%Rental置成相同的數值(如有多於一個有值就取最大的)
如:
分店 開始日(租約) 結束日(租約) 開始日(租期) 結束日(租期) Rental % Rental
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 1200 3%
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3%
100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3%

解决方案 »

  1.   


    Update t Set [%Rental]=(Select Max([%Rental]) From t s Where s.分店=t.分店 And s.开始日=t.开始日 And s.结束日=t.结束日 And IsNull(s.[%Rental],'')<>'')
      

  2.   

    mustudent理解錯我的意思了
    這裡需要的是 租期相同and分店相同 就用同組內MAX(% Rental) 來UPDATE 同組所有 % Rental
    原始數據如下:
    分店 開始日(租約) 結束日(租約) 開始日(租期) 結束日(租期) Rental % Rental
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 1200 0
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 1
    100 2000-1-1 2011-04-31 2000-6-1 2000-12-30 5000 0
    100 2000-1-1 2011-04-31 2001-1-1 2008-1-31 0 10
    100 2000-1-1 2011-04-31 2001-1-1 2008-1-31 3500 0
    100 2000-1-1 2011-04-31 2008-2-1 2011-04-31 0 7
    100 2000-1-1 2011-04-31 2008-2-1 2011-04-31 2000 0
    101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 1000 0
    101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 0 5需要獲得數據如下:
    分店 開始日(租約) 結束日(租約) 開始日(租期) 結束日(租期) Rental % Rental
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 1200 3
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3
    100 2000-1-1 2011-04-31 2000-1-1 2000-5-30 0 3
    100 2000-1-1 2011-04-31 2000-6-1 2000-12-30 5000 0
    100 2000-1-1 2011-04-31 2001-1-1 2008-1-31 0 10
    100 2000-1-1 2011-04-31 2001-1-1 2008-1-31 3500 10
    100 2000-1-1 2011-04-31 2008-2-1 2011-04-31 0 7
    100 2000-1-1 2011-04-31 2008-2-1 2011-04-31 2000 7
    101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 1000 5
    101 2010-3-1 2011-04-31 2010-3-1 2011-04-31 0 5create table #aa
    (
    brhcode int,
    start_date_1 datetime,
    end_date_1 datetime,
    start_date_2 datetime,
    end_date_2 datetime,
    retal_1 int,
    retal_2 int
    )
    --drop table #aa
    --delete from #aainsert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 1200,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 0,3 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 0,1 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-6-1', '2000-12-30', 5000,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1', '2008-1-31', 0,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1','2008-1-31', 0, 0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1','2008-1-30', 3500, 0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1', '2011-4-30', 0, 7 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1', '2011-4-30', 2000, 0 )insert into #aa values(101, '2000-1-1', '2011-5-30', '2001-1-1', '2011-5-30', 1000, 0 )
    insert into #aa values(101, '2000-1-1', '2011-5-30', '2001-1-1', '2011-5-30', 0, 5 )
      

  3.   


    update #aa set retal_2=a.Retal
    From(Select MAX(retal_2) Retal,brhcode,start_date_2,end_date_2 from #aa group by brhcode,start_date_2,end_date_2)a
    where #aa.brhcode =a.Retal and Datediff(day,#aa.start_date_1,a.start_date_2)=0 and datediff(day,#aa.end_date_2,a.end_date_2)=0
    --这样?
      

  4.   

    mustudent 用你的第二個方法UPDATE到0行數據。
    修改了一下表數據,現在應該會清晰一點。前面的兩個日期是整個租約的開始~結束時間,第3/4個日期是租約內分拆的租期insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 1200,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 0,3 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-1-1', '2000-5-30', 0,1 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2000-6-1', '2000-12-30', 5000,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1', '2008-1-31', 0,0 )
    insert into #aa values(100, '2000-1-1', '2011-4-30', '2001-1-1','2008-1-31', 3500, 0 )
    insert into #aa values(100, '2008-2-1', '2011-4-30', '2008-2-1', '2011-4-30', 0, 7 )
    insert into #aa values(100, '2008-2-1', '2011-4-30', '2008-2-1', '2011-4-30', 2000, 0 )insert into #aa values(101, '2000-1-1', '2011-5-30', '2001-1-1', '2011-5-30', 1000, 0 )
    insert into #aa values(101, '2000-1-1', '2011-5-30', '2001-1-1', '2011-5-30', 0, 5 )
      

  5.   

    OK了,你關聯的地方寫錯了一個@@,我改成#aa.brhcode = a.brhcode 就好了。感謝你mustudent:
    SQL codeupdate #aa set retal_2=a.Retal
    From(Select MAX(retal_2) Retal,brhcode,start_date_2,end_date_2 from #aa group by brhcode,start_date_2,end_date_2)a
    where #aa.brhcode =a.Retal and Datediff(day,#aa.start_date_1,a.start_date_2)=0 and datediff(day,#aa.end_date_2,a.end_date_2)=0
    --这样?
      

  6.   


    create table t1
    (
    name int,
    start1 datetime,
    end1 datetime,
    start2 datetime,
    end2 datetime,
    total1 int,
    total2 varchar(10)
    )
    insert into t1
    select 100, '2000-1-1', '2011-04-30', '2000-1-1', '2000-5-30', 1200,null union all
    select 100, '2000-1-1', '2011-04-30', '2000-1-1', '2000-5-30', 0, '3%' union all
    select 100, '2000-1-1', '2011-04-30', '2000-1-1', '2000-5-30', 0, '1%' union all
    select 100, '2000-1-1', '2011-04-30', '2000-6-1', '2000-12-30', 5000,null union all
    select 100, '2000-1-1', '2011-04-30', '2001-1-1', '2008-1-31', 3500,null union all
    select 100, '2000-1-1', '2011-04-30', '2008-2-1', '2011-04-30', 2000,null union all
    select 101, '2010-3-1', '2011-04-30', '2010-3-1', '2011-04-30', 1000,null union all
    select 101, '2010-3-1', '2011-04-30', '2010-3-1', '2011-04-30', 0, '5%'
    select * from t1;with abc as
    (select name,start2,end2,MAX(total2) as total2 from t1 group by name,start2,end2)
    update t1 set total2=ISNULL(abc.total2,0) from t1 inner join abc 
    on t1.name=abc.name and t1.start2=abc.start2 and t1.end2=abc.end2