在另一个-SQL select语句中使用一个计算的产品

在另一个-SQL select语句中使用一个计算的产品,sql,select,Sql,Select,这是我冗长的SQL代码。我基本上是在尝试首先将数据库信息从每月、每日和每年的费率转换为小时费率。然后将该小时数转换为美元(如果尚未转换) 到目前为止,我有一个转换后的计量单位汇率作为“转换汇率”,但在转换成美元时如何使用这个新转换的汇率?即: 当wo.货币=英镑时,则(wo.平均汇率*1.66) 我无法将wo.avg_率更改为wo.converted_率、x.converted_率、“converted rate”或“converted rate”。我如何才能做到这一点 SELECT

这是我冗长的SQL代码。我基本上是在尝试首先将数据库信息从每月、每日和每年的费率转换为小时费率。然后将该小时数转换为美元(如果尚未转换)

到目前为止,我有一个转换后的计量单位汇率作为“转换汇率”,但在转换成美元时如何使用这个新转换的汇率?即:

当wo.货币=英镑时,则(wo.平均汇率*1.66)

我无法将wo.avg_率更改为wo.converted_率、x.converted_率、“converted rate”或“converted rate”。我如何才能做到这一点

SELECT 
    x.work_order_ref as 'Work Order Reference'
    ,x.buyer_code as 'Buyer Code'
    ,x.submit_time as 'Work Order Submit Time'
    ,x.rate_unit_for_spend_calc as 'UOM'
    ,x.avg_rate as 'ST Rate'
    ,x.converted_rate as 'Converted Rate'
    ,x.currency as 'Currency'
    ,x.converted_amount AS 'Converted Amount'
    ,jp.title AS 'JP Title'
    ,lbt.name AS 'Labor Type'
    ,jp.max_distribution_level AS 'Max Distr Level'
    ,si.city AS 'City'
    ,si.state_province as 'State'
    ,si.country AS 'Country'
    ,ctg.name AS 'Category'
    ,count(jpd.supplier_code) as 'Suppliers'
from 
(select 
         wo.work_order_ref
         ,wo.buyer_code
         ,wo.submit_time
         ,wo.rate_unit_for_spend_calc 
         ,"converted_rate" = 
                        CASE
                        WHEN wo.rate_unit_for_spend_calc='Hr' then wo.avg_rate
                        WHEN wo.rate_unit_for_spend_calc='MO' then (wo.avg_rate/173.333)
                        WHEN wo.rate_unit_for_spend_calc='Day' then (wo.avg_rate/8)
                        WHEN wo.rate_unit_for_spend_calc='Yr' then (wo.avg_rate/2080)
                        END
         ,wo.avg_rate
         ,wo.currency
         ,wo.job_posting_id
         ,wo.site_id
         ,wo.worker_id
         ,wo.uploaded_flag
         ,wo.sequence
         ,"converted_amount" =
                        CASE
                        WHEN wo.currency='USD' then (wo.avg_rate)
                        WHEN wo.currency='GBP' then (wo.avg_rate*1.66)
                        WHEN wo.currency='SEK' then (wo.avg_rate*0.14)
                        WHEN wo.currency='EUR' then (wo.avg_rate*1.31)
                        WHEN wo.currency='CAD' then (wo.avg_rate*0.92)
                        WHEN wo.currency='AUD' then (wo.avg_rate*0.93)
                        WHEN wo.currency='INR' then (wo.avg_rate*0.017)
                        WHEN wo.currency='MXN' then (wo.avg_rate*0.076)
                        WHEN wo.currency='COP' then (wo.avg_rate*0.00052)
                        WHEN wo.currency='BRL' then (wo.avg_rate*0.44)
                        WHEN wo.currency='MYR' then (wo.avg_rate*0.32)
                        WHEN wo.currency='CNY' then (wo.avg_rate*0.16)
                        WHEN wo.currency='RON' then (wo.avg_rate*0.3)
                        WHEN wo.currency='CZK' then (wo.avg_rate*0.047)
                        WHEN wo.currency='SGD' then (wo.avg_rate*0.8)
                        WHEN wo.currency='PLN' then (wo.avg_rate*0.31)
                        WHEN wo.currency='JPY' then (wo.avg_rate*0.0096)
                        WHEN wo.currency='NZD' then (wo.avg_rate*0.84)
                        WHEN wo.currency='CHF' then (wo.avg_rate*1.09)
                        --ELSE (wo.avg_rate*x.conversion_factor)
                        END
    from work_order wo(nolock) ) as x


如果能让我更好地发布完整代码和/或我得到的内容,我也可以这样做。

如果将转换存储在表中,查询就会容易得多。您甚至可以摆脱内部查询:

declare @uom table (
    unit char(3) not null primary key,
    divisor decimal(10, 5) not null
);

insert into @uom values
    ('Hr', 1), ('MO', 173.333), ('Day', 8), ('Yr', 2080);

declare @rate table (
    currency char(3) not null primary key,
    usd_mult decimal(10, 5) not null
);

insert into @rate values
    ('USD', 1), ('GBP', 1.66), ('SEK', 0.14), ('EUR', 1.31),
    ('CAD', 0.92), ('AUD', 0.93), ('INR', 0.017), ('MXN', 0.076),
    ('COP', 0.00052), ('BRL', 0.44), ('MYR', 0.32), ('CNY', 0.16),
    ('RON', 0.3), ('CZK', 0.047), ('SGD', 0.8), ('PLN', 0.31),
    ('JPY', 0.0096), ('NZD', 0.84), ('CHF', 1.09);

select 
    wo.work_order_ref,
    wo.buyer_code,
    wo.submit_time,
    wo.rate_unit_for_spend_calc,
    wo.avg_rate / u.dividend as converted_rate,
    wo.avg_rate,
    wo.currency,
    wo.job_posting_id,
    wo.site_id,
    wo.worker_id,
    wo.uploaded_flag,
    wo.sequence,
    -- is this where you wanted converted_rate * currency_mult?
    wo.avg_rate / u.dividend * r.usd_mult as converted_amount
from 
    work_order wo with (nolock)
        inner join
    @uom u
        on wo.rate_unit_for_spend_calc = u.unit
        inner join
    @rate r
        on wo.currency = r.currency
)
否则,可以将计算隐藏在函数后面。这使内容更易于阅读,但会降低查询速度,因为优化器无法看穿以下函数:

create function dbo.HourlyRate(@unit as char(3), @rate as decimal(10, 5)) returns decimal(10, 5) as
begin
    return case @unit
        when 'Hr' then @rate
        when 'MO' then @rate / 173.333
        when 'Day' then @rate / 8
        when 'Yr' then @rate / 2080
    end
end

create function dbo.ConvertToUSD(@currency as char(3), @amount as decimal(20, 5)) returns decimal(20, 5) as
begin
    return case @currency
        when 'USD' then @amount
        when 'GBP' then @amount * 1.66
        ...
    end
end
这样说吧:

select
    ...
    dbo.HourlyRate(wo.rate_unit_for_spend_calc, wo.avg_rate) as converted_rate,
    ...
    dbo.ConvertToUSD(wo.currency, dbo.HourlyRate(wo.rate_unit_for_spend_calc, wo_avg_rate) as converted_amount,
    ...
from
    work_order wo with (nolock)

我不清楚问题是什么。“不能将wo.avg_率更改为wo.converted_率、x.converted_率、“converted rate”或“converted rate”是什么意思?为了您未来的理智,请将转换率放入表格中。您的想法不太确定,但这似乎是一个糟糕的方法。更好地尝试功能,它提供了更多的灵活性。正如@Laurence所说,使用汇率换算表。