database - Multiple aggregates from same sub query in SQL Server -
Is there a better way of doing this without repeated sub queries that select a different field?
select name, er.DateEventStarts, e.LocationName, (select count (*) from tickets T where went to TkDeleted = 0 and return = 0 and t. EventRepetitionID = er.EventRepetitionID) as NoOfAttendees, select (sum (t.TicketTotalCost) where tickets T went to TkDeleted = 0 and return = 0 and t.EventRepetitionID = er.EventRepetitionID) as NoOfAttendees select ticket t (amount (t.OnlinePayFee) Where t.Deleted = 0 and return went to = 0 and t.EventRepetitionID = er.EventRepetitionID) (amount as OnlinePayFee, ticket T (TkOnlinePayTotalCost Select where) t.Deleted = 0 and use It went to Mr. = 0 and t.EventRepetitionID = er.EventRepetitionID) OnlinePayTotalCost the [event] e Join er.EventRepetitionID = on EventRepetition er (where select ER2 EventRepetition top 1 EventRepetitionID er2.EventID = e.EventID) I am sorry if it has already been answered but I can not think of a way to describe this problem, so that was unable to find a solution.
this can do the trick:
SELECT Name, er DateEventStarts, e.LocationName, t.NoOfAttendees, t.TotalTickets, t.OnlinePayFee, [event t.OnlinePayTotalCost] e er.EventRepetitionID = on EventRepetition er JOIN (where select ER2 EventRepetition top 1 EventRepetitionID er2.EventID = e.EventID ) join (EventRepetitionID selection, COUNT (*), SUM (TicketTotalCost), SUM (OnlinePayFee), SUM (OnlinePayTotalCost) was deleted from the ticket = 0 and = 0 group back EventRepetitionID) T (EventRepetitionID, NoOfAttendees , TotalTickets, OnlinePayFee, OnlinePayTotalCost) on ER.EventRepetitionID = T.EventRepetitionID This scans the ticket table only once Should Rni.
Comments
Post a Comment