0

嗨,我被困在计算列中。我厌倦了在谷歌上搜索类似的问题并且没有运气。

请看下表。

配置表

-------------------------------
|KeyName        | KeyValue    |
-------------------------------
|MinimumMargin  | .50         |
|MaximumMargin  | 2           |
-------------------------------

价格表

-------------------------------
|SKU  |Price   | Shipping     |
-------------------------------
|001  | 23    | 1.5         |
|002  | 20    | 1.5         |
-------------------------------

我想得到如下结果:

SELECT SKU, SUM(PRICE+SHIPPING) + 'ConfigTable.MinMargin' as CalcPrice FROM PRICE

提前致谢。

4

1 回答 1

2

您可以使用子查询来获取sum每个的价格和运费sku,然后使用 aCROSS JOIN加入configTable来添加keyValue

select p.sku,
  p.CalcPrice + (c.KeyValue) CalcPrice
from
(
  select p.sku,
    sum(p.price + p.shipping) CalcPrice
  from priceTable p
  group by p.sku
) p
cross join configTable c
where c.keyname = 'MinimumMargin'

请参阅SQL Fiddle with Demo

如果KeyValue是 varchar,那么您需要将其转换,以便将其添加到CalcPrice

select p.sku,
  p.CalcPrice + convert(c.KeyValue, decimal(10, 2)) CalcPrice
from
(
  select p.sku,
    sum(p.price + p.shipping) CalcPrice
  from priceTable p
  group by p.sku
) p
cross join configTable c
where c.keyname = 'MinimumMargin';

请参阅带有演示的 SQL Fiddle

于 2013-03-07T10:43:03.483 回答