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 |
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2009-09-28 : 15:08:02
|
Table1 has 3 locations = pickat, loadunload, deliverto, depottable2 has a fromLocation, toLocation, miles code not workingSELECT c.pickat, c.loadunload, c.deliverto, SUM(c.miles1 + c.miles2)FROM(SELECT a.pickat, a.loadunload, a.deliverto, b.miles as miles1FROM actual_moves a Inner join point2point As b on a.pickat = b.fromLocation AND a.loadunload = b.tolocation UNION ALLSELECT a.picakt, a.loadunload, a.deliverto, b.miles as miles2FROM actual_moves a Inner join point2point As b on a.loadunload = b.fromLocation AND a.deliverto = b.tolocation) cGRoup by c.pickat, c.loadunload, c.deliverto, c.miles |
|
|
rweasel6
Starting Member
6 Posts |
Posted - 2009-09-28 : 15:17:13
|
| If Im not mistaken once your do the unition you dont have a column for Miles2 anymore. You are trying to perform a column to column calculation but your end query only sees Miles once.You may want to add a dummy Miles1 to your first select statment and make it 0 then add a dummy Miles 2 to the second one and make it a 0.SELECT c.pickat, c.loadunload, c.deliverto, SUM(c.miles1 + c.miles2)FROM(SELECT a.pickat, a.loadunload, a.deliverto, b.miles as miles1, 0 as miles2FROM actual_moves a Inner join point2point As b on a.pickat = b.fromLocation AND a.loadunload = b.tolocation UNION ALLSELECT a.picakt, a.loadunload, a.deliverto, 0 as miles1, b.miles as miles2FROM actual_moves a Inner join point2point As b on a.loadunload = b.fromLocation AND a.deliverto = b.tolocation) cGRoup by c.pickat, c.loadunload, c.deliverto, c.miles |
 |
|
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2009-09-28 : 15:18:51
|
Sample DATAtable1 Pickat | loadunload | deliverto | depot1 2 3 0581 50 1 0581 34 1 0583 49 1 058table2MoveID|fromLocation|toLocation|miles1 1 2 218 2 1 50 5963 1 34 1504 3 49 1365 2 3 526 50 1 5967 34 1 1508 49 1 12 Expected ResultsPickat | loadunload | deliverto | totalmiles1 2 3 270 --218 + 521 50 1 1192 --596 + 5961 34 1 300 --150 + 1503 49 1 148 --136 + 12 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2009-09-28 : 15:30:43
|
Good point.. I want to return the expected results from the 2 sample tables..I would like to select pickat, loadunload, deliverto, and a sum of the miles between the pickat/loadunload move & the loadunload/deliveto move. total miles of tripthanksquote: Originally posted by tkizer What is your question?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog"Let's begin with the premise that everything you've done up until this point is wrong."
|
 |
|
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2009-09-28 : 16:47:35
|
| [code]SELECT FromLocation, Tolocation, SUM(c.miles)FROM(SELECT FromLocation, Tolocation, milesfrom point2point as awhere FromLocation = 'Am-Cell - Charleston SC' AND tolocation = 'Eka Chem. Inc. - Augusta GA' UNION SELECT FromLocation, Tolocation, milesfrom point2point as bwhere fromlocation = 'Eka Chem. Inc. - Augusta GA' AND toLocation = 'Wando Term. - Charleston SC')cGroup by fromlocation, tolocation[/code]ok Ive narrowed it down, the above returns[code]Am-Cell - Charleston SC |Eka Chem. Inc. - Augusta GA | 139.300003051758Eka Chem. Inc. - Augusta GA | Wando Term. - Charleston SC | 144.600006103516[/code]I need[code]Am-Cell - Charleston SC | Eka Chem. Inc. - Augusta GA | Wando Term. - Charleston SC | 283.900009155273[/code] |
 |
|
|
|
|
|
|
|