| Author |
Topic |
|
GrandVizier
Starting Member
14 Posts |
Posted - 2007-05-03 : 18:44:15
|
HejBack for some more advise on creating a valid select statement. Here is sample data and the query I'm working with:DECLARE @Agent TABLE (aID INT, aName VARCHAR(7))INSERT @AgentSELECT 35, 'HAN' UNION ALLSELECT 36, 'OLD' UNION ALLSELECT 37, 'BOAT' DECLARE @Reservations TABLE (rID INT, rTourID INT, rAgentID INT, rPass INT, rChild INT, rCoup INT, rTotal MONEY)INSERT @ReservationsSELECT 10, 64, 35, 5, 0, 0, 50 UNION ALLSELECT 11, 60, 35, 1, 0, 0, 10 UNION ALLSELECT 12, 64, 36, 2, 1, 0, 25.7 UNION ALLSELECT 13, 62, 37, 0, 3, 2, 8.56 UNION ALLSELECT 14, 64, 37, 2, 0, 1, 34 UNION ALLSELECT 15, 66, 35, 6, 0, 0, 43.75 DECLARE @Tour TABLE (tID INT, tName VARCHAR(10), tPickupID INT)INSERT @TourSELECT 60, 'Bike', 1 UNION ALLSELECT 62, 'Bus', 3 UNION ALLSELECT 64, 'Heli', 2 UNION ALLSELECT 66, 'Walk', 1DECLARE @Pickup TABLE (pID INT, pName VARCHAR(15))INSERT @PickupSELECT 1, '1st Pickup' UNION ALLSELECT 2, '2nd Pickup' UNION ALLSELECT 3, 'Adv Pickup' SELECT Pickup.pName, Agent.aName, SUM(Res.rPass) AS Adl, SUM(Res.rChild) AS Chl, SUM(Res.rCoup) AS A_CPN, SUM(Res.rPass + Res.rChild) AS PAX, SUM(Res.rTotal) AS Sales, Tour.tPickupIDFROM @Agent AS Agent INNER JOIN @Reservations AS Res ON Agent.AID = Res.rAgentID INNER JOIN @Tour AS Tour ON Tour.tID = Res.rTourID join @Pickup AS Pickup ON Tour.tPickupID = Pickup.pIDGROUP BY Pickup.pName, Agent.aName, Tour.tPickupIDORDER BY Agent.aName The output of that is looking like this:pName aName Adl Chl A_CPN PAX Sales tPickupID2nd Pickup BOAT 2 0 1 2 34.00 2Adv Pickup BOAT 0 3 2 3 8.56 31st Pickup HAN 7 0 0 7 53.75 12nd Pickup HAN 5 0 0 5 50.00 22nd Pickup OLD 2 1 0 3 25.70 2 The way I want it to look is more like:pName aName Adl Chl A_CPN PAX Sales tPickupID1st Pickup BOAT 0 0 0 0 0.00 1 2nd Pickup BOAT 2 0 1 2 34.00 2Adv Pickup BOAT 0 3 2 3 8.56 31st Pickup HAN 7 0 0 7 53.75 12nd Pickup HAN 5 0 0 5 50.00 2Adv Pickup HAN 0 0 0 0 0.00 31st Pickup OLD 2 1 0 3 0.00 1 2nd Pickup OLD 2 1 0 3 25.70 2Adv Pickup OLD 0 0 0 0 0.00 3 I've tried a couple techniques for creating a temp table to fill in, but I just seem to be getting the same results.One method that I haven't been able to get to work but seems logical is to create a temp table with the above result columns, insert the default values for each row, then update the temp table with an aggregate function.But since I'm probably making this much more difficult then I should be, I'm here looking for advise.Thanks |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-03 : 18:50:48
|
| best bet is probably to have default rows in your results table and then update them according to your wishes.because this kind of logic if i understand it correctly isn't worth the complexity of writing in a single statement._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-04 : 00:33:45
|
| DECLARE @Agent TABLE (aID INT, aName VARCHAR(7))INSERT @AgentSELECT 35, 'HAN' UNION ALLSELECT 36, 'OLD' UNION ALLSELECT 37, 'BOAT' DECLARE @Reservations TABLE (rID INT, rTourID INT, rAgentID INT, rPass INT, rChild INT, rCoup INT, rTotal MONEY)INSERT @ReservationsSELECT 10, 64, 35, 5, 0, 0, 50 UNION ALLSELECT 11, 60, 35, 1, 0, 0, 10 UNION ALLSELECT 12, 64, 36, 2, 1, 0, 25.7 UNION ALLSELECT 13, 62, 37, 0, 3, 2, 8.56 UNION ALLSELECT 14, 64, 37, 2, 0, 1, 34 UNION ALLSELECT 15, 66, 35, 6, 0, 0, 43.75 DECLARE @Tour TABLE (tID INT, tName VARCHAR(10), tPickupID INT)INSERT @TourSELECT 60, 'Bike', 1 UNION ALLSELECT 62, 'Bus', 3 UNION ALLSELECT 64, 'Heli', 2 UNION ALLSELECT 66, 'Walk', 1DECLARE @Pickup TABLE (pID INT, pName VARCHAR(15))INSERT @PickupSELECT 1, '1st Pickup' UNION ALLSELECT 2, '2nd Pickup' UNION ALLSELECT 3, 'Adv Pickup' Select pa.pName , pa.aName, Coalesce(Sum(r.rPass), 0) as Adl, Coalesce(Sum(r.rChild), 0) as Chl, Coalesce(Sum(r.rCoup), 0) as A_Cpn, Coalesce(Sum(r.rTotal), 0.0000) as Sales, t.tPickupIDfrom (Select * from @pickup p cross join @Agent pic) pa left outer join @tour t on pa.pid = t.tPickupID left outer join @Reservations r on r.rTourID = t.tid and r.rAgentID = pa.aid and r.rTourID = t.tid group by t.tPickupID, pa.pName,pa.aName order by pa.aname |
 |
|
|
GrandVizier
Starting Member
14 Posts |
Posted - 2007-05-04 : 12:23:53
|
ah see, there it is - thats exactly what I was looking for - thanks so muchof course now that I've come to implement it, I've found what I actually needed was a cross-tab report - so I've made the adjustments to get what I need but since this is new syntax for me I'm posting it here in case it could be simplified even more...Selectpa.aName,Coalesce((SUM(CASE t.tPickupID WHEN 1 THEN r.rPass ELSE 0 END)), 0) AS Ad1,Coalesce((SUM(CASE t.tPickupID WHEN 1 THEN r.rChild ELSE 0 END)), 0) AS Ch1,Coalesce((SUM(CASE t.tPickupID WHEN 2 THEN r.rPass ELSE 0 END)), 0) AS Ad2,Coalesce((SUM(CASE t.tPickupID WHEN 2 THEN r.rChild ELSE 0 END)), 0) AS Ch2,Coalesce((SUM(CASE t.tPickupID WHEN 3 THEN r.rPass ELSE 0 END)), 0) AS Ad3,Coalesce((SUM(CASE t.tPickupID WHEN 3 THEN r.rChild ELSE 0 END)), 0) AS Ch3,Coalesce(Sum(r.rCoup), 0) as A_Cpn,Coalesce(Sum(r.rPass + r.rChild), 0) as Pax,Coalesce(Sum(r.rTotal), 0.0000) as Salesfrom(Select * from @pickup p cross join @Agent pic) paleft outer join @tour ton pa.pid = t.tPickupIDleft outer join @Reservations ron r.rTourID = t.tid and r.rAgentID = pa.aid and r.rTourID = t.tidgroup by pa.aNameorder by pa.aname Thanks again |
 |
|
|
|
|
|