0

我正在尝试将某些记录放入第二个查询的子集中。我不断收到的错误是:“表别名多次使用:Eqanswer - SQL 语句中的错误位置是:199(行:3 列:22)”这只是最后一条消息,我错过了明显的答案解决它,但我真的很想知道这是否可行。

    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

真的Eqanswer两次:

FROM Eqanswer, entities
LEFT JOIN Eqanswer 

编辑

好的,这个子选择在很多地方都是错误的:(我会将我的评论添加为字符串,而不是评论,以使它们更加突出)

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 回答
0
SELECT 
        e.entityid, e.entitynum, e.entityrole, eq.memotext, eq.fieldnum
    FROM 
        ( SELECT 
            DISTINCT entitynum 
         /* THIS NEEDS TO BE CHANGED {Eqanswer} used 2 times without any alias */
          FROM Eqanswer, entities
                LEFT JOIN Eqanswer 
                    ON (Eqanswer.entitynum = entities.entitynum) 
          WHERE 
                entities.partyID LIKE  /* why you need this ?? */
                    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
于 2013-04-23T14:07:09.457 回答