2012-01-17 6 views
1

私はレコードの数を選択するには、次のクエリを書いている:私は今何したいのですがどのようなこれを一括更新するにはどうすればよいですか?

select  
    b.building_name , 
    lp.lease_id , 
    lp.suite_id , 
    lp.suite_name , 
    lp.tenant_trading_name , 
    sco.scca_uplift as 'Current GST Uplift Jan 2011' , 
    nu.new_uplift as 'GST Uplift Jan 2011 Post-Change' , 
    lp.scca_broad_category_code , 
    lp.scca_sales_group_code , 
    lp.scca_fine_categories_code , 
    lp.scca_notes_code 
    , '(lease_id = ''' + lp.lease_id + ''' and suite_id =''' 
     + lp.suite_id + ''') or' as 'Base SQL Output' 
from   
    property.lease_period lp 
     inner join property.property b 
      on b.building_id = lp.building_id 
     inner join 
     (
      select scca_uplift , 
        scca_broad_category_code , 
        scca_fine_categories_code , 
        scca_sales_group_code 
      from property.lease_period 
      where period_id = 252 
     ) sco 
      on sco.scca_broad_category_code = lp.scca_broad_category_code 
       and sco.scca_sales_group_code = lp.scca_sales_group_code 
       and sco.scca_fine_categories_code = lp.scca_fine_categories_code 

     inner join 
     (
      -- Department Store 
      select '10' as scca_broad_category_code, 
      '100'  as scca_sales_group_code, 
      '1000'  as scca_fine_categories_code, 
      109.8  as new_uplift 
      -- Discount Department Stores 
      union all 
      select '10' as scca_broad_category_code, 
      '110'  as scca_sales_group_code, 
      '1100'  as scca_fine_categories_code, 
      109.5  as new_uplift 
      -- Supermarket 
      union all 
      select '10' as scca_broad_category_code, 
      '130'  as scca_sales_group_code, 
      '1300'  as scca_fine_categories_code, 
      105.0  as new_uplift 
      -- Newsagents and Stationary 
      union all 
      select '30' as scca_broad_category_code, 
      '340'  as scca_sales_group_code, 
      '3403'  as scca_fine_categories_code, 
      110.0  as new_uplift 
      -- Discount Variety 
      union all 
      select '30' as scca_broad_category_code, 
      '350'  as scca_sales_group_code, 
      '3502'  as scca_fine_categories_code, 
      109.5  as new_uplift 
      -- Optometrist 
      union all 
      select '30' as scca_broad_category_code, 
      '380'  as scca_sales_group_code, 
      '3801'  as scca_fine_categories_code, 
      110.0  as new_uplift 

     ) nu 
      on  nu.scca_broad_category_code = lp.scca_broad_category_code 
        and nu.scca_sales_group_code = lp.scca_sales_group_code 
        and nu.scca_fine_categories_code = lp.scca_fine_categories_code 
where 
    (
     -- Department Store 
     ( lp.scca_broad_category_code = '10' and 
      lp.scca_sales_group_code = '100' and 
      lp.scca_fine_categories_code = '1000' and 
      lp.scca_notes_code in ('10001', '10002')) 
     -- Discount Department Store 
     or 
     ( lp.scca_broad_category_code = '10' and 
      lp.scca_sales_group_code = '110' and 
      lp.scca_fine_categories_code = '1100' and 
      lp.scca_notes_code in ('11001', '11002', '11003', '11004')) 
     -- Supermarket 
     or 
     ( lp.scca_broad_category_code = '10' and 
      lp.scca_sales_group_code = '130' and 
      lp.scca_fine_categories_code = '1300' and 
      lp.scca_notes_code in ('13001', '13002', '13003', '13004', '13005', 
            '13006', '13007', '13007', 
            '13008', '13009', '13010', '13011', '13012', 
            '13013', '13014')) 
     -- Newsagents and Stationary 
     or 
     ( lp.scca_broad_category_code = '30' and 
      lp.scca_sales_group_code = '340' and 
      lp.scca_fine_categories_code = '3403') 
     -- Discount Variety 
     or 
     ( lp.scca_broad_category_code = '30' and 
      lp.scca_sales_group_code = '350' and 
      lp.scca_fine_categories_code = '3502') 
     -- Optometrist 
     or 
     ( lp.scca_broad_category_code = '30' and 
      lp.scca_sales_group_code = '380' and 
      lp.scca_fine_categories_code = '3801') 

    ) 
    and  b.building_id <> 42000 
    and  lp.scca_uplift is not null 
    and  lp.period_id >= 252 
order by  
    b.building_name , 
    scca_broad_category_code , 
    scca_sales_group_code , 
    scca_fine_categories_code , 
    scca_notes_code 

はnu.new_uplift列を取り、ことを各行に適用されますクエリを記述で現在表示されている値で表示されています(混乱する文章、別の方法で説明する必要がある場合は教えてください)。私はCTEを使用してこれを行うことができると思ったが、私はサブクエリを使用しているので可能だとは思わない。そのことを念頭に置いて - 私はここで何を利用すべきですか?

答えて

2

非常に広い意味で、UPDATE... from...がここで動作するはずです。 1つの可能なテンプレート:

SELECT文と同様に、「OtherTableName」はほとんどの表のosサブクエリで置き換えることができます。

ここでは、更新するテーブルを選択し、「from」句に最初にリストされたものとして配置し、ORDER BY句なしのモンスターサブクエリに適切に結合します。

+0

素晴らしいです。どうもありがとうございました。 – Codingo

0

フィリップのポストされたアプローチは、はるかに優れているが、私は(利益のために掲載)この変更を適用するには、次を使用:

begin transaction 

update property.lease_period 
set  scca_uplift = 
        (
         case 
          when -- Department Store 
           (
            scca_notes_code in ('10001', '10002') and 
            scca_broad_category_code = '10' and 
            scca_sales_group_code = '100' and 
            scca_fine_categories_code = '1000' 
           ) 
          then '109.8' 
          when -- Discount Department Store 
           (
            scca_notes_code in ('11001', '11002', '11003', '11004') and 
            scca_broad_category_code = '10' and 
            scca_sales_group_code = '110' and 
            scca_fine_categories_code = '1100' 
           ) 
          then '109.5' 
          when -- Supermarket 
           (
            scca_notes_code in ( '13001', '13002', '13003', '13004', '13005', 
                  '13006', '13007', '13007', 
                  '13008', '13009', '13010', '13011', '13012', 
                  '13013', '13014') and 
            scca_broad_category_code = '10' and 
            scca_sales_group_code = '130' and 
            scca_fine_categories_code = '1300' 
           ) 
          then '105.0' 
          when -- Newsagents and Stationary 
           (
            scca_broad_category_code = '30' and 
            scca_sales_group_code = '340' and 
            scca_fine_categories_code = '3403' 
           ) 
          then '110.0' 
          when -- Discount Variety 
           (
            scca_broad_category_code = '30' and 
            scca_sales_group_code = '350' and 
            scca_fine_categories_code = '3502' 
           ) 
          then '109.5' 
          when -- Optometrist 
           (
            scca_broad_category_code = '30' and 
            scca_sales_group_code = '380' and 
            scca_fine_categories_code = '3801' 
           ) 
          then '110.0' 
         end 
        ) 
where 
    period_id >= 252 and 
    building_id <> 42000 and 
    scca_uplift is not null 

rollback transaction 
関連する問題