嵌套SQL查询返回太多行

时间:2015-05-30 16:21:52

标签: sql sql-server

我有两张桌子 - 活动和今晚推动季节性活动列表系统。事件保存所有季节性事件级别数据,而Tonight保存与事件将运行的每个日期相关的数据。

我需要以嵌套的方式查询这两个表,首先从Tonight表中过滤掉一些行,然后根据之前的Tonight级别过滤仅显示每个事件一次。

我目前正试图通过先从Tonight表中选择所有相关的事件级别ID然后使用这些来恢复相关的事件行来实现此目的。这些行需要连接到Tonight Table和另一个POI表以返回所有相关数据。

问题在于,由于Tonight表上的连接,我的基于ID的方法为每个事件带回了多行。我显然是以错误的方式解决这个问题,但我不知道如何纠正这些问题。你能告诉我吗?

谢谢,

PJ

SELECT 
    Events.ID AS ID, 
    Events.EventName AS EventName,
    Events.EventAlias AS EventAlias, 
    Events.EntryPrice AS EventPrice, 
    Events.TicketPrice AS TicketPriceText, 
    Events.MusicStyle AS MusicStyle, 
    Events.EventType AS EventType, 
    Events.CanSellEntry AS EventsCanSellEntry,
    Tonight.CanSellEntry AS TonightCanSellEntry
FROM Events
JOIN Tonight AS Tonight ON Events.Id = Tonight.Event_Id 
WHERE Events.ID in 
(
    SELECT DISTINCT Events.ID AS ID 
    FROM Events 
    JOIN PlacesOfInterest AS PoI ON Events.POI_Id = PoI.Id 
    JOIN Tonight AS Tonight ON Events.Id = Tonight.Event_Id 
    WHERE 
        Tonight.EventDate >= '30-May-2015' AND 
        Tonight.EventDate <= '31-Dec-2015' AND 
        Events.CanSellEntry <> 'no' AND 
        Events.EntryPrice > 0 AND 
        Tonight.CanSellEntry <> 'no' AND 
        Events.EventType = 'night'
) 
ORDER BY Events.EventName

0 个答案:

没有答案