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 > Database Server Software > Sybase > Complex SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-12, 09:24
prami prami is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Design help on complex SQL

Hi there

I hope someone could get me a help.

I have to create an regular extract out of my SQL database.
The extract is collecting data from many different tables, as usual .. ;-)

What I do first is to collect the keys of data records I need to extract (already that is going over various tables with various criterias ..) into a temp table.
Nevertheless that's working well.

In next steps, I complete temp table by adding additional fields and filling them.
In one of this steps, I have now the problem, how to do it best ...

Let my try to explain.
I have a master table country_places like
country, place
CH, A
CH, B
CH, C
AT, D
AT, E
FR, G
...

Then I have a routing table, where I combine order items with place movements, like
order, item, movement sequence, from place, to place
order_1, 1, 1, A, B
order_1, 1, 2 B, D
order_1, 2, 1, A, B
order_1, 2, 2 B, D
order_2, 1, 1, A, G

What I want is to get
- the last place of same country where order starts
- even if item sequence not always starts with 1
- and even if movement sequence may vary from order item to order item


What I did so far is:
SELECT r.from_place
FROM routing r
JOIN key_collection k ON k.order = r.order

WHERE r.from_place IN ( SELECT place FROM country_places cp WHERE cp.country = k.from_country )
AND r.item = ( SELECT MIN(item)
FROM routing r2
WHERE r2.order = r.order
AND r2.from_place IN ( SELECT place from country_places cp WHERE cp.country = k.from_country )
AND r2.movement_sequence = ( SELECT MAX(movement_sequence)
FROM routing r3
WHERE r3.order = r2.order
AND r3.from_place IN ( SELECT place from country_places cp WHERE cp.country = k.from_country )
)

Imaginge, that I have around 200'000 rows in my key_collection.

I'm getting nuts .....

Any help and ideas welcome.


Thanks a lot

P.S. Sysbase Adaptive Server Enterprise/12.5.4

Last edited by prami; 01-25-12 at 09:40.
Reply With Quote
  #2 (permalink)  
Old 01-27-12, 03:29
prami prami is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Hi

no-one a tip how to make it faster & better ..... ?

Thx
Wolfgang
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