来自SQL Server中相同子查询的多个聚合

时间:2011-09-02 08:32:09

标签: database sql-server-2008 join subquery

有没有更好的方法可以做到这一点,而不需要重复的子查询只选择一个不同的字段?

SELECT Name, er.DateEventStarts, e.LocationName,
(SELECT count(*) FROM Ticket t WHERE t.Deleted = 0 AND Refunded = 0 AND t.EventRepetitionID = er.EventRepetitionID) AS NoOfAttendees,
(SELECT sum(t.TicketTotalCost) FROM Ticket t WHERE t.Deleted = 0 AND Refunded = 0 AND t.EventRepetitionID = er.EventRepetitionID) AS NoOfAttendees,
(SELECT sum(t.OnlinePayFee) FROM Ticket t WHERE t.Deleted = 0 AND Refunded = 0 AND t.EventRepetitionID = er.EventRepetitionID) AS OnlinePayFee,
(SELECT sum(t.OnlinePayTotalCost) FROM Ticket t WHERE t.Deleted = 0 AND Refunded = 0 AND t.EventRepetitionID = er.EventRepetitionID) AS OnlinePayTotalCost  
FROM [Event] e
JOIN EventRepetition er ON er.EventRepetitionID = (SELECT TOP 1 EventRepetitionID FROM         EventRepetition er2 WHERE er2.EventID = e.EventID)

正如您所看到的,子查询几乎相同,但只是对另一行求和或计数。我觉得必须有更好的方法来做到这一点,但看不出怎么做。我想这可能效率不高。

如果已经回答这个问题,我很抱歉,但我想不出一种方法来描述这个问题,所以无法搜索解决方案。

2 个答案:

答案 0 :(得分:1)

这可能会起到作用:

SELECT
    Name,
    er.DateEventStarts,
    e.LocationName,
t.NoOfAttendees,
t.TotalTickets,
t.OnlinePayFee,
t.OnlinePayTotalCost  
FROM [Event] e
JOIN EventRepetition er
    ON
        er.EventRepetitionID = (SELECT TOP 1 EventRepetitionID FROM EventRepetition er2 WHERE er2.EventID = e.EventID)
join
    (select EventRepetitionID,COUNT(*),SUM(TicketTotalCost),SUM(OnlinePayFee),SUM(OnlinePayTotalCost)
    from Ticket
    where Deleted = 0 and Refunded = 0
    group by EventRepetitionID) t (EventRepetitionID,NoOfAttendees,TotalTickets,OnlinePayFee,OnlinePayTotalCost)
        on
            er.EventRepetitionID = t.EventRepetitionID

这应该只扫描票证表一次。

答案 1 :(得分:0)

未测试下一个,但它将是这样的:

select Q.name, Q.ateEventStarts, Q.locationName,
        count(*), sum(T.TicketTotalCost), sum(T.TicketTotalCost), 
        sum(T.OnlinePayFee), sum(T.OnlinePayTotalCost)
    from ( 
        select Name, er.DateEventStarts, e.LocationName, er.EventRepetitionID
            from Event e JOIN EventRepetition er on er.EventRepetitionID = 
                (select top 1 EventRepetitionID from         
                    EventRepetition er2 where er2.EventID = e.EventID) ) Q
    left outer join Ticket T
        on t.Deleted = 0 and T.Refunded = 0 
            and t.EventRepetitionID = Q.EventRepetitionID