If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Advanced queries

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-10, 15:28
ChrisSD ChrisSD is offline
Registered User
 
Join Date: Apr 2010
Posts: 2
Advanced queries

Hi,

I'm a bit of a database noob, so I was wondering if someone here could help. I can't post the actual problem I'm working on, but i hope the following hypothetical example will illustrate what I'm trying to do.

Take this table:
Code:
fromAccountNo  |  toAccountNo
         4563  |         2276
         2276  |         2666
         2276  |         3798
         2276  |         2100
         2276  |         4563
         2666  |         1249
...
It shows how money has moved through a number of accounts. E.g. cash has gone from #4563 to #2276 and then on to other accounts. Some has gone back again. Whilst most accounts will be connected in this way, there will also be accounts that are unconnected to most the others.

What I want to be able do is pick an account (say #4563) and list all the accounts that link to it and be able to track backwards from any account to #4563. I'd also like to be able state how many steps separate an arbitrary account from #4563, but I suppose I can work that out easily enough when I track backwards.

I'm currently using programming loops to do this (which requires many database queries), but I wondered if it is possible to do it in one SQL query and if I should do so.

Sorry if this isn't very clear, I'm not the best at explaining things. I'll try to clarify it more if anyone asks.
Reply With Quote
  #2 (permalink)  
Old 04-21-10, 16:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you can do it with LEFT OUTER JOINs, one account link per join

which database are you planning to use?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-21-10, 16:51
ChrisSD ChrisSD is offline
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks for the reply. I'm currently planning on using sqlite.
Reply With Quote
  #4 (permalink)  
Old 04-21-10, 17:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sorry, not familiar with it, but i will venture a guess that it doesn't have recursive SQL

therefore you will have to code as many LEFT OUTER JOINs as necessary to cover the longest chain of links that you want to investigate
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On