0

2 番目のクエリのサブセットに特定のレコードを取得しようとしています。私が取得し続けるエラーは次のとおりです。それを解決することはできませんが、これが機能するかどうかを本当に知りたいです。

    SELECT 
        e.entityid, e.entitynum, e.entityrole, eq.memotext, eq.fieldnum
    FROM 
        ( SELECT 
            DISTINCT entitynum 
          FROM Eqanswer, entities
                LEFT JOIN Eqanswer 
                    ON (Eqanswer.entitynum = entities.entitynum) 
          WHERE 
                entities.partyID LIKE
                    CASE 
                        WHEN (entities.partyID LIKE '%Joe%' 
                            OR entities.partyID LIKE '%Bob%' 
                            OR entities.partyID LIKE '%Bill%') 
                        THEN
                            (eqanswer.entityrole = 'F_TL' 
                            AND (CONVERT(eqanswer.memotext, sql_date)=curDate() -5 ))
                        ELSE
                            (eqanswer.entityrole = 'F_TL' AND eqanswer.fieldnum = 160 
                            AND (CONVERT(eqanswer.memotext,sql_date) = CurDate() -1 ))
                        END  
        ) AS bs  

        LEFT JOIN Entities e ON (bs.entitynum = e.entitynum)
        LEFT JOIN Eqanswer eq ON (bs.entitynum = eq.entitynum)
    WHERE((eq.entityrole = 'R_CKLIST' AND eq.fieldnum in (8,9,10,11,34,35))
    OR (eq.entityrole = 'F_TL' AND eq.fieldnum in (104,112,158,160))
    OR (eq.entityrole = 'C_REVIEW' AND eq.fieldnum = 69))
    ORDER BY e.entitynum

次の両方を試しました。

    SELECT 
    e.entityid, e.entitynum, e.entityrole, eq.memotext, eq.fieldnum
    FROM ( SELECT 
        DISTINCT entitynum 
            FROM Eqanswer, entities
            LEFT JOIN Eqanswer 
                ON (Eqanswer.entitynum = entities.entitynum) 
      WHERE 
            entities.partyID 
                CASE 
                    WHEN (entities.partyID LIKE '%Joe%' 
                        OR entities.partyID LIKE '%Bob%' 
                        OR entities.partyID LIKE '%Bill%') 
                    THEN
                        (eqanswer.entityrole = 'F_TL' 
                        AND eqanswer.fieldnum = 160
                        AND (CONVERT(eqanswer.memotext, sql_date)=curDate() -5 ))
                    ELSE
                        (eqanswer.entityrole = 'F_TL' AND eqanswer.fieldnum = 160 
                        AND (CONVERT(eqanswer.memotext,sql_date) = CurDate() -1 ))
                    END  
    ) AS bs  

    LEFT JOIN Entities e ON (bs.entitynum = e.entitynum)
    LEFT JOIN Eqanswer eq ON (bs.entitynum = eq.entitynum)
    WHERE((eq.entityrole = 'R_CKLIST' AND eq.fieldnum in (8,9,10,11,34,35))
    OR (eq.entityrole = 'F_TL' AND eq.fieldnum in (104,112,158,160))
    OR (eq.entityrole = 'C_REVIEW' AND eq.fieldnum = 69))
    ORDER BY e.entitynum

    SELECT e.entityid, e.entitynum, e.entityrole, e.thestatus, eq.memotext, eq.dateentered, eq.datechgd, eq.fieldnum, e.docloc 
    FROM (SELECT DISTINCT eqanswer.entitynum FROM eqanswer
      LEFT JOIN entities ON (Eqanswer.entitynum = entities.entitynum) 
      WHERE entities.partyID
          CASE WHEN (entities.party3ID LIKE '%Joe%' 
                    OR entities.party3ID LIKE '%Bob%' 
                    OR entities.party3ID LIKE '%Bill%') 
                    THEN
          CASE WHEN  (eqanswer.entityrole = 'F_TL' 
             AND eqanswer.fieldnum = 160 
             AND (CONVERT(eqanswer.memotext, sql_date)=curDate() -5 ))
             THEN 1 ELSE 0 END
          ELSE
          CASE WHEN (eqanswer.entityrole = 'F_TL' 
             AND eqanswer.fieldnum = 160 
             AND (CONVERT(eqanswer.memotext,sql_date) = CurDate() -1 ))
          THEN 1 ELSE 0 END
          END = 1
      ) AS bs  
   LEFT JOIN Entities e ON (bs.entitynum = e.entitynum)
   LEFT JOIN Eqanswer eq ON (bs.entitynum = eq.entitynum)
   WHERE((eq.entityrole = 'R_CKLIST' AND eq.fieldnum in (8,9,10,11,34,35))
   OR (eq.entityrole = 'F_TL' AND eq.fieldnum in (104,112,158,160))
   OR (eq.entityrole = 'C_REVIEW' AND eq.fieldnum = 69))
   ORDER BY e.entitynum

どちらもエラーをスローしていますが、それは構文エラーのようなものです (私のせいです)。

4

2 に答える 2

5

あなたは本当に2回持っていますEqanswer

FROM Eqanswer, entities
LEFT JOIN Eqanswer 

編集

OK、このサブセレクトは非常に多くの場所で間違っています: (コメントをより目立たせるために、コメントではなく文字列として追加します)

SELECT  entitynum 
FROM Eqanswer, entities
LEFT JOIN Eqanswer             'this is the duplicate table name'
    ON (Eqanswer.entitynum = entities.entitynum) 
WHERE entities.partyID LIKE    
    'LIKE implies that partyID AND the CASE expression are both strings'
    CASE 
        WHEN (entities.partyID LIKE '%Joe%' 
                        OR entities.partyID LIKE '%Bob%' 
                        OR entities.partyID LIKE '%Bill%') 
        THEN (eqanswer.entityrole = 'F_TL' 
             AND (CONVERT(eqanswer.memotext, sql_date)=curDate() -5 ))
             'but THIS is a boolean expression'
        ELSE (eqanswer.entityrole = 'F_TL' 
             AND eqanswer.fieldnum = 160 
             AND (CONVERT(eqanswer.memotext,sql_date) = CurDate() -1 ))
             'and THIS too'
    END  
) AS bs  

したがって、LIKE は不明なデータ型の列で動作し、CASE はブール式になります。ほとんどの場合、この行entities.partyID LIKEは単なるコピー アンド ペースト エラーです (うまくいけば)。無視しましょう。

CASE 句の結果としてブール式を返すことはできません。したがって、整数を返すように CASE を書き直し、この値を比較して意図した WHERE 条件と一致させる必要があります。

おそらく次のようなものです:

WHERE 
    CASE 
        WHEN (entities.partyID LIKE '%Joe%' 
                        OR entities.partyID LIKE '%Bob%' 
                        OR entities.partyID LIKE '%Bill%') 
        THEN
            CASE WHEN  (eqanswer.entityrole = 'F_TL' 
                 AND (CONVERT(eqanswer.memotext, sql_date)=curDate() -5 ))
            THEN 1 ELSE 0 END
        ELSE
            CASE WHEN (eqanswer.entityrole = 'F_TL' 
                 AND eqanswer.fieldnum = 160 
                 AND (CONVERT(eqanswer.memotext,sql_date) = CurDate() -1 ))
            THEN 1 ELSE 0 END
    END = 1
于 2013-04-23T14:01:33.367 に答える