0

レシピ (より一般的なカテゴリ) に関連付けることができる構成データのテーブルがあります。

このような:

レシピ:

ID Name
1 Default
2 A
3 B

設定データ

ID equipment ParentID RecipeID
1  3420      1         1 
2  3420      1         2
3  3421      1         1
4  3421      1         2
5  3422      1         1

レシピ「A」のすべての構成データを選択し、レシピ A が構成データ行に構成されていない場合に「デフォルト」値にフォールバックする方法を知りたいです。次に、これを取得します:

ID equipment ParentID RecipeID
2  3420      1         2
4  3421      1         2
5  3422      1         1

私はすでにこのようなものを見つけましたが、それが良いアプローチであるかどうかはわかりません:

select * from ConfigurationData
where RecipeID=2 and parentID=1
union 
select * from ConfigurationData
where RecipeID=1 and parentID=1
and Equipment not in (select Equipment from ConfigurationData 
where RecipeID=2 and parentID=1)
4

2 に答える 2

0

CTEを使用してこのようなものはありますか?

/** 
 * first declare an id to search for
 */
declare @recipeId int 

set @recipeId = 2 -- Change this to the recipe you want to select

/**
 * Now the actual query
 */
with cte as (
  select r.id as Recipe, c.id, c.equipment, c.recipeid, row_number() over (partition by r.id, c.equipment order by c.RecipeId desc) as row
  from
       recipes r inner join configuration c on r.id = c.recipeid or c.recipeid = 1
)
select
   id, equipment, recipeid
   from cte 
   where row = 1 
   and r.id = @recipeId
于 2013-05-21T09:03:32.263 に答える