2016-07-26 81 views
0

我收到了不明確定義的列。輸入這個SQL語句後。 我想要做的是計算BusinessEventInvite和EventInvite之間參加每個事件的人數。從不同的表中計數2個相同的屬性

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended 
FROM EVENT E 
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid 
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID 
WHERE EI.ATTENDED = Attended and BEI.ATTENDED = Attended 
GROUP BY E.EVENTID 
ORDER BY E.EVENTID ASC; 

BEI表

ALUMNIID EVENTID RSVP ATTENDED

1   6 YES  Attended 
2   6 YES  Attended 
3   6 YES  Attended 
4   6 YES  Attended 
5   6 YES  Attended 
6   6 NO     

6行選擇

EI表 ALUMNIID EVENTID RSVP ATTENDED

7   6 YES  Attended 
8   6 YES  Attended 
9   6 No     
10   7 YES  Attended 
11   8 YES  Attended 
12   7 YES  Attended 
13   7 YES  Attended 
14   8 YES  Attended 
15   7 YES  Attended 
16   8 YES  Attended 

個10行中選擇

事件表

EVENTID EVENTNAME


 6 Annual Social Night   
    7 Annual Social Night   
    8 Annual Social Night   
    9 Annual Social Night   
+0

我假設您擁有BUSINESSEVENTINVITE和EVENTINVITE中的同一個人,並在您的查詢中計算了兩次。你想要這個人數一兩次嗎? –

+0

每個人都擁有一個唯一的ID,因此不可能有2個相同的記錄。 –

回答

0

這可能導致你的WHERE聲明如下

WHERE EI.ATTENDED = Attended and BEI.ATTENDED = Attended 

這或許應該是

WHERE EI.ATTENDED = 'Attended' AND BEI.ATTENDED = 'Attended'; 

考慮更改您的查詢,並從WHERE移動條件JOIN ON

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended 
FROM EVENT E 
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid 
AND BEI.ATTENDED = 'Attended' 
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID 
AND EI.ATTENDED = 'Attended' 
GROUP BY E.EVENTID 
ORDER BY E.EVENTID ASC; 
+0

EI.Attended和BEI.Atended是一個具有不同變量的列。 –

+0

@WeiJun,如果有幫助,請參閱編輯答案。你應該首先澄清這一點。 – Rahul

1

你需要顯示錶的架構是一定的,如果那些表中存在這些列。但是一般來說,當SQL Server在您的查詢中使用的兩個或多個表中找到相同的列名時,您會遇到該錯誤,因此它要求您通過使用別名對其進行限定來解決該歧義。

我的猜測是,你需要改變這一行

WHERE EI.ATTENDED = Attended and BEI.ATTENDED = Attended 

WHERE EI.ATTENDED = BEI.Attended 
0

ALUMNIID EVENTID RSVP無人值班


 1   6 YES  Attended 
    2   6 YES  Attended 
    3   6 YES  Attended 
    4   6 YES  Attended 
    5   6 YES  Attended 
    6   6 NO     

6行中選擇

ALUMNIID EVENTID RSVP無人值班


 7   6 YES  Attended 
    8   6 YES  Attended 
    9   6 No     
    10   7 YES  Attended 
    11   8 YES  Attended 
    12   7 YES  Attended 
    13   7 YES  Attended 
    14   8 YES  Attended 
    15   7 YES  Attended 
    16   8 YES  Attended 

10行中選擇

第一個表是BEI和第二表是EI

+0

如果我說得對,你需要得到預期的結果是重寫你的WHERE子句,如下所示:WHERE EI.ATTENDED ='出席'和BEI.ATTENDED ='出席' –

+0

你應該編輯你的問題並添加輔助信息在它不是在污染。首先讀你的人很疑惑。 –

+0

我試過了(WHERE EI.ATTENDED ='出席'和BEI.ATTENDED ='出席'),但他們似乎給了我不同的結果。 –

0

試試這個,因爲你沒有給參加該表從?因爲現在有三張桌子。

SELECT DISTINCT E.EVENTID, Count(EI.Attended)+Count(BEI.Attended) as TotalAttended 
FROM EVENT E 
left outer join BUSINESSEVENTINVITE BEI ON E.EVENTID= BEI.eventid 
left outer join EVENTINVITE EI on EI.EVENTID = E.EVENTID 
WHERE EI.ATTENDED = BEI.Attended and BEI.ATTENDED = EI.Attended 
GROUP BY E.EVENTID 
ORDER BY E.EVENTID ASC; 
0

模棱兩可的列錯誤是由於因爲屬性出席在這個查詢多個表不存在被識別爲where語句您的別名。您可能的意思是在您的平等聲明中使用字符串'Attended'。

WHERE EI.ATTENDED = 'Attended' and BEI.ATTENDED = 'Attended' 

這將返回誰參加了這兩次事件,誰被邀請

WHERE EI.ATTENDED = 'Attended' or BEI.ATTENDED = 'Attended' 

您可能想看看你的結果與和「或」語句產生用戶

它很難確定什麼您有或正在嘗試使用有限的表格/數據結構

+0

每個活動的商務和校友出席者總數。 –

相關問題