我正在尝试将某些记录放入第二个查询的子集中。我不断收到的错误是:“表别名多次使用: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
两者都抛出错误,但这就像语法错误(我的错)。