-1
select
    CustomerName,City,State, MatchId,ServingSize, Fruit, DarkGreen, 
    [Red/Orange], Legumes, Starchy, Other, [Grains (oz)], 
    [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)], Calories, 
    [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
    MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name, 
    WebsiteLinkToSource, Date
from
    #FinalTempTable
Where 
    (
        MatchId,ServingSize, Fruit, DarkGreen, [Red/Orange], Legumes, Starchy, 
        Other, [Grains (oz)], [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)],
        Calories, [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
        MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name,
        WebsiteLinkToSource, Date
    )
    IN 
    (
        Select
            MatchId,ServingSize, Fruit, DarkGreen, [Red/Orange], Legumes, 
            Starchy, Other, [Grains (oz)], [Whole51%orgreater?], [Meat / Meat Alt], 
            [Fluid Milk (cups)],Calories, [Sodium (MG)], [Saturated Fat (grams)], 
            [Trans Fat (grams)], MeetsAllianceForAHealthierGenerationGuidelines, 
            OtherNotes, Name, WebsiteLinkToSource, Date
        From
            MealContributions 
        Where
            MatchId IN (Select distinct MatchId from #FinalTempTable)
    )

このクエリを使用していますが、次のエラーが発生します。

メッセージ 4145、レベル 15、状態 1、行 10 ',' の近くで、条件が予想されるコンテキストで指定された非ブール型の式。

#finaltemptable のすべての列と、MatchId が #finaltemptable の matchid と同じである食事の貢献の列のみが必要です

4

1 に答える 1

1

フォーム

Where 
        (MatchId,ServingSize, Fruit, ... other columns .... ) 
IN 
        (... subquery with columns ...)

MySQL では機能しますが、SQL Server では機能しません。

リストされたすべての一致する列で EXISTS 句を作成する必要があります。

 select CustomerName, City, State, MatchId, ServingSize, Fruit, DarkGreen, 
        [Red/Orange], Legumes, Starchy, Other, [Grains (oz)], 
        [Whole51%orgreater?], [Meat / Meat Alt], [Fluid Milk (cups)], Calories, 
        [Sodium (MG)], [Saturated Fat (grams)], [Trans Fat (grams)],
        MeetsAllianceForAHealthierGenerationGuidelines, OtherNotes, Name, 
        WebsiteLinkToSource, Date
   from #FinalTempTable f
  where exists (
        select *
          from MealContributions m
         where f.matchid = m.matchid AND
               f.ServingSize = m.ServingSize AND
               .... all the columns! ...)

最後の条件

Where MatchId IN (Select distinct MatchId from #FinalTempTable))

すでに一致しているため、無関係ですf.matchid = m.matchid

于 2012-11-21T21:34:36.897 に答える