使用基于表值的查找的SQL语句

时间:2014-05-27 09:25:04

标签: sql sql-server dynamic lookup

我需要手工构建一个复杂的SQL语句(对我来说至少很复杂!)

我有三张桌子,下面是结构。

客户表: 客户ID,客户名称,地址,关税*

APRW14_FFA68878_Calls: CLI,ChargeCode,持续时间,时间段

(关税名称) - 具有不同关税的多个表格 ChargeCode,Peak,Off-Peak,Weekend,Setup,Minimum

*每个客户都有自己的关税,如客户表中所定义 - 然后有一个相应的关税表,其中包含关税明细。

我要做的是写一个动态语句,以便查询从customer表中查找资费名称,然后从相应的Tariff表中提取数据。

到目前为止,这是我的代码。

UPDATE [APRW14_FFA68878_Calls] 
SET    [APRW14_FFA68878_Calls].[Sell Price] = [*Query to pull back tariff name from Customer table here*].[Peak]/60*[APRW14_FFA68878_Calls].[Duration (secs)]
FROM   [Test].[dbo].[DaisyRates_May2014]
INNER JOIN [DaisyBilling].[dbo].[APRW14_FFA68878_Calls] on [DaisyBilling].[dbo].[APRW14_FFA68878_Calls].[ChargeCode] = [Test].[dbo].[*Query to pull back tariff name from Customer table here*].[Chargecode]

我已将" 查询从此处的客户表中提取关税名称"在我需要根据Customer表撤回Tariff名称的代码中。

我希望这是有道理的,任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

使用像这样的伪代码存储过程...

select distinct tariff from customer table;
for each tariff name as current tariff name
    update customer set (select ... from current tariff name table)... 
    where customer.tariff = current tariff name
end for
相关问题