我有一個可以工作的查詢,但會返回所有學生,無論他們是否下訂單。我只想要那些有訂單的人。查詢工作正常,直到我添加where子句。我該如何正確寫這個?SQL Server where子句順序計數> 0
SELECT top 100 percent s.id, s.fname as [First Name], s.lname as [Last Name],
(select count(student_id) from orderX x where x.student_id=s.id) as [Order Count],
(select sum(no_attendees) from orderX x where x.student_id=s.id) as [Attendees/Participants],
(select sum(eventHours) from orderX x where x.student_id=s.id) as [Event Hours],
oc1.text as [Occupation 1], oc2.text as [Occupation 2],
oc3.text as [Occupation 3], s.OccupationOther, s.dateGraduated, s.organization, s.city, s.zip, s.st, s.county,
aud.text as [Preferred Audience], pts.text as [Plans to Share], mr.text as [Main Reason]
FROM student s
left join occupation1 oc1 on s.Occupation1 = oc1.id
left join occupation2 oc2 on s.Occupation2 = oc2.id
left join occupation3 oc3 on s.Occupation3 = oc3.id
left join audience aud on s.audience = aud.id
left join PlanToShare pts on s.PlanToShare = pts.id
left join mainReason mr on s.mainReason = mr.id
where [Order Count] > 0
什麼查詢...你確定沒有其他方式? –
我知道這是醜陋的,但做的工作〜! – user2055729
'Where語句不能識別你的列名。使用完整的查詢計數 – Fabio