ITrying to get 2 separate select statements to show in one result set using a user defined variable. Should I use a Union or With somewhere here?
SET @X1 =
((SELECT SUM(ef.Fee * (rd.ActualReturn - ra.startdate)) as "total revenue from rentals"
FROM EqRentDetails as rd
JOIN EquipmentInventory as ei ON rd.ItemSKU = ei.ItemSKU
JOIN EqRentAgreement as ra ON rd.RentalID = ra.RentalID
JOIN customers ON customers.CustomerID = ra.CustomerID
JOIN EquipmentFee as ef ON ef.EquipmentID = ei.EquipmentID and customers.CategoryKey =
ef.Customercategory
WHERE rd.Actualreturn IS NOT NULL
GROUP BY ra.CustomerID));
SET @Y =
(SELECT SignUp.ParticipantID, SUM(avt.TripFee) as "Total revenue from trip fees"
FROM SignUp
JOIN Offerings as o on o.OfferingCode = SignUp.OfferingCode
JOIN AvailableTrips as avt on avt.TripID = o.TripCode
JOIN Customers on Customers.CustomerID = SignUp.ParticipantID
group by SignUp.ParticipantID);
#for display
Select @Y, @X;
2
Answers
Well you can just add the two queries in a single Select statement. This way you can reduce some extra SQL code execution. Most of the case it does not matter but some cases it may work as optimization. Here is a sample