Я написал следующий запрос для выбора количества записей:
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, но я не верю, что это возможно, учитывая, что я использую подзапросы для своих объединений.Имея это в виду - что я должен использовать здесь?