简体   繁体   English

SQL工作从左到右直到找到值

[英]SQL Work left to right until Value Found

I've been scratching my head at this but cannot find an 'elegant' way to do it. 我一直对此scratch之以鼻,但找不到“优雅”的方式来做到这一点。 I'm using MS SQL Server 2012. 我正在使用MS SQL Server 2012。

Basically, I'm been given a table (ItemsBySeason) that contains a bunch of items. 基本上,我得到了一个包含一堆项目的表(ItemsBySeason)。 The length of the actual items will either be 11 (planning entity) or 7 (style) characters in length. 实际项目的长度将是11个(计划实体)或7个(样式)字符。

I then have another table (PriceLists) with items and prices. 然后,我还有另一个表(PriceLists),其中包含项目和价格。 Again these have items with a price, and the items will be both 7 and 11 characters on length. 同样,这些商品包含有价格的商品,商品长度分别为7和11个字符。

My question is, is there a function out there that would read through each item in ItemBySeason and check if there is a price in the PriceLists table that matches at 11 characters in length, if not then use the price for the 7 character item? 我的问题是,是否有一个函数可以通读ItemBySeason中的每个项目,并检查PriceLists表中是否有价格匹配11个字符的价格,如果没有,则将价格用于7个字符的项目?

For example: 例如:

Table: Item By Season
Item: AABBCCD1234

Table: PriceLists
Item: AABBCCD1234
Price: 10

So in this instance it would I could retrieve the 10 for AABBCCD1234 因此在这种情况下,我可以为AABBCCD1234检索10

However, if the below happened: 但是,如果发生以下情况:

Table: Item By Season
Item: AABBCCD8888

No match for 'AABBCCD8888' in PriceLists but the below exists: 价目表中“ AABBCCD8888”不匹配,但存在以下内容:

Table: PriceLists
Item: AABBCCD
Price: 12

How do I check that the 7 length exists and pull through 12? 如何检查7个长度是否存在并拉穿12个?

Any help would be greatly appreciated. 任何帮助将不胜感激。 The only work around I've found is to create a temp table of every single Item that doesn't have a price at the planning entity level and bring all those through at the style level, then append to the PriceLists table, but this seems long winded. 我发现的唯一解决方法是为在规划实体级别没有价格的每个单个项目创建一个临时表,并在样式级别使所有这些临时表通过,然后追加到PriceLists表,但这似乎wind。

Thanks, 谢谢,

Michael 迈克尔

SELECT item_by_season.item
     , planning_entity_prices.price A planning_entity_price
     , style_prices.price As style_price
     , Coalesce(planning_entity_prices.price, style_prices.price) As price
FROM   item_by_season
 LEFT
  JOIN price_lists As planning_entity_prices
    ON planning_entity_prices.item = item_by_season.item
   AND Len(planning_entity_prices.item) = 11
 LEFT
  JOIN price_lists As style_prices
    ON style_prices.item = item_by_season.item
   AND Len(style_prices.item) = 7
;

Here is a method that uses a correlated subquery (or equivalently it could use cross apply ). 这是一种使用相关子查询的方法(或等效地,它可以使用cross apply )。 If something matches both the 7 and 11 length strings, it chooses the one with 11. 如果同时匹配7和11个长度的字符串,则选择11个字符串。

select i.*,
       (select top 1 pl.price
        from PriceLists pl
        where pl.itemid = i.itemid or
              left(i.itemid, 7) = pl.itemid
        order by len(pl.itemid) desc
       ) as price
from ItemsBySeason i ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM