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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Cross Join

Author  Topic 

markpirvine
Starting Member

3 Posts

Posted - 2010-01-12 : 11:47:18
Hi,

I have a problem ordering data within a cross join query. The cross join operates on a table called job and engineer. This produces a list of all job and engineers (lat/lon the job address and engineer start location is used to calculate the distance). The result set below shows a sample of this data:

job_id job_location eng_id eng_location distance
578 BT48 25 BT47 0.062985279
578 BT48 30 BT47 0.062985279
578 BT48 50 BT48 0.062985279
578 BT48 51 BT47 0.062985279
578 BT48 52 BT47 0.062985279
578 BT48 182 BT47 0.062985279
578 BT48 21 BT47 0.640291847
578 BT48 27 BT82 9.804417058
578 BT48 16 BT82 9.804417058
578 BT48 75 BT49 12.42933314
578 BT48 15 BT82 13.79810824
578 BT48 18 BT49 15.91910151
578 BT48 59 BT51 24.12364445
578 BT48 13 BT51 24.14147537
578 BT48 189 BT51 26.80170492
578 BT48 190 BT51 26.80170492
578 BT48 58 BT52 27.95470444
578 BT48 43 BT80 28.32651161
578 BT48 46 BT52 28.54226644
578 BT48 181 BT56 30.82336946
578 BT48 186 BT53 31.92369962
578 BT48 65 BT71 40.68487732
578 BT48 5 BT71 40.91659532
578 BT48 187 BT42 42.61219686
578 BT48 183 BT43 43.06784521
578 BT48 33 BT42 43.11139737
578 BT48 42 BT43 45.1397925
578 BT48 61 BT74 46.77587542
578 BT48 62 BT29 50.94823572
578 BT48 55 BT61 50.99844949

At present I have an order by clause on distance (ASC). Typically this produces over 20,000 rows - this distance between each job and engineer is calculated. However some jobs and engineers are the same distance apart - the first six results all have the same distance - this is fine. However as this job is located is BT48 I would like the first result to eng_id 50:

job_id job_location eng_id eng_location distance
578 BT48 50 BT48 0.062985279
578 BT48 25 BT47 0.062985279
578 BT48 30 BT47 0.062985279
578 BT48 51 BT47 0.062985279
578 BT48 52 BT47 0.062985279
578 BT48 182 BT47 0.062985279

Is this possible? Any help would be greatly appreciated.

Mark

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-12 : 12:00:16
May be this?
order by distance,case when eng_location = job_location then 0 else 1 end, eng_id
Go to Top of Page

markpirvine
Starting Member

3 Posts

Posted - 2010-01-12 : 12:06:50
Hi,

Thanks for the prompt response - works perfectly!

Mark
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-12 : 12:14:28
You're welcome
Go to Top of Page
   

- Advertisement -