1

各子テーブルのすべてのIDを持つ1つのマスターテーブルがあります。SQLステートメントは次のようになります...

SELECT   Class.Descript
       , Regulation.Descript AS Reg
       , Compgroup.Descript AS Grouping
       , Category.Descript AS Cat
       , Exempt.Descript AS Exempt
       , Reason.Descript AS Reasons
       , COALESCE(ComponentRuleSet.NormalType, ComponentRuleSet.Supertype, '') AS Type
FROM     ComponentRuleSet 
LEFT OUTER JOIN Reason 
   ON ComponentRuleSet.ComponentCategoryID = Reason.ComponentCategoryID 
LEFT OUTER JOIN Class 
   ON ComponentRuleSet.ComponentClassID = Class.ComponentClassID
LEFT OUTER JOIN Regulation
   ON ComponentRuleSet.RegulationID = Regulation.RegulationID 
LEFT OUTER JOIN Compgroup
   ON ComponentRuleSet.ComplianceGroupID = Compgroup.ComplianceGroupID
LEFT OUTER JOIN Category 
   ON ComponentRuleSet.ComponentCategoryID = Category.ComponentCategoryId
LEFT OUTER JOIN Exempt 
   ON ComponentRuleSet.ExemptID = Exempt.ComponentExemptionID
WHERE (ComponentRuleSet.ComponentID = 38048)

問題は、ComponentRuleSetテーブルにNormalTypeSupertypeという2つのフィールドがあることです。これらのフィールドのいずれかに値がある場合は、 Typeという列に表示する必要があります。ただし、どちらにも値がない場合は、[タイプ]列に空白の値を表示する必要があります。何か案は?

- -編集

編集したクエリでのCOALESCEの配置は正しいですか?それでもエラーが返されます。

- アップデート

重要:両方のフィールドのタイプはブール値です。TRUE値を保持する列の列名を返し、その値をTYPE列に配置する必要があります。

4

2 に答える 2

3

COALESCEこのフィールドの使用:

COALESCE(ComponentRuleSet.NormalType, ComponentRuleSet.Supertype, '') AS Type

COALESCE

引数の中で最初のnull以外の式を返します。


実際の要件に関するコメントに従うことCASEは、おそらくより良いオプションです。

CASE WHEN ComponentRuleSet.NormalType = 1 THEN 'NormalType'
     WHEN ComponentRuleSet.Supertype = 1 THEN 'SuperType'
     ELSE ''
END AS Type
于 2012-10-27T21:24:37.533 に答える
1

あなたのコメントを見ると、おそらくCASE式が機能するでしょう:

select ...
      , CASE WHEN ComponentRuleSet.NormalType is not null then 'NormalType'
             WHEN ComponentRuleSet.Supertype  is not null then 'SuperType'
             ELSE ''
             end as Type

更新ブール値はtrueの場合は1、falseの場合は0なので、次のことを試してください。

select ...
      , CASE WHEN ComponentRuleSet.NormalType = 1 then 'NormalType'
             WHEN ComponentRuleSet.Supertype  = 1 then 'SuperType'
             ELSE ''
             end as Type
于 2012-10-27T22:05:56.657 に答える