Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
marly
Starting Member
6 Posts |
Posted - 2008-06-14 : 18:39:13
|
| I have three tables. QuoteQuoteID, QuoteNumber, TransportationTransportationID, QuoteID, Item, Description, CostOptionalChargesOptionalChargesID, QuoteID, Item, Description, CostWhat I want to do is SUM(Cost) for the Transportation and the Optional Charges table. If I do a normal INNER JOIN, with the SUM for Cost on the table Transportation and Optional Charges, it will SUM each twice. For example if Transportation has three rows with a cost of 10, 20, 30, it will SUM a total of 120 instead of 60.So I came up with: Select Quote.QuoteID, QuoteDate, t.TransportationTotalCostFROM Quote.QuoteINNER JOIN( SELECT QuoteID, SUM(COST) AS TransportationTotalCost FROM Quote.Transportation GROUP BY QuoteID) t on Quote.QuoteID = t.QuoteID WHERE Quote.QuoteID = 135Which gives me the total for the Transportation Table, but how do I go about adding in the Optional Charges Table? Thanks, marly |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-06-15 : 02:48:41
|
| Try this oneSelect Quote.QuoteID, QuoteDate, t.TransportationTotalCostFROMQuote.QuoteINNER JOIN(SELECT QuoteID, SUM(Transportation.COST) AS TransportationTotalCost,SUM(OptionalCharges.COST) AS OptionalChargesTotalCostFROMTransportationINNER JOIN OptionalChargesON Transportation.QuoteID=OptionalCharges.QuoteIDGROUP BYTransportation.QuoteID,OptionalCharges.QuoteID) ton Quote.QuoteID = t.QuoteIDWHERE Quote.QuoteID = 135 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-15 : 03:08:56
|
| You could probably post some sample data from the tables to give us an idea of how the tables are related as we dont know if relation is 1 to 1 or 1 to many? |
 |
|
|
|
|
|
|
|