We have a parent table, Journal and a child table, Stops.
A Journal can have X amount of Stops.
For a good 80% of our SQL queries, our customers are just interested in the first and last Stop for each Journal.
We often have queries that do something like this:
SELECT
MINStop.PhysicalCity AS MinStopCity,
MAXStop.PhysicalCity AS MaxStopCity,
MINMax.DocNo AS JournalDocNo
FROM (
SELECT
MIN(Stops.StopsPkey) AS MINStopPkey,
MAX(Stops.StopsPkey) AS MAXStopPkey,
Stops.JournalPkey,
Journal.DocNo
FROM Journal
LEFT OUTER JOIN Stops ON Journal.JournalPkey=Stops.JournalPkey
WHERE Journal.Type=150
GROUP BY Stops.JournalPkey, Journal.DocNo) MINMax
LEFT OUTER JOIN Stops MINStop ON MINStop.StopsPkey=MINMax.MINStopPkey
LEFT OUTER JOIN Stops MAXStop ON MAXStop.StopsPkey=MINMax.MAXStopPkey
It works fine, but definitely suffers at the larger locations.
It's just how it's always been done here, and I wanted to reach out for possible improvements. Would it be worth it to store the first and last Stop data in the Journal table, even if that data is duplicated?
We use Advantage Database 11.x so are a bit limited on the SQL features. Probably our only route would be through table modifications.
http://devzone.advantagedatabase.com/forum– Lennart - Slava Ukraini Dec 23 '19 at 23:53