thanks for the hints,
my question regarding "more optimized way" was if it can be overwritten to support ANSI SQL for a potential db migration? I was using mysql until now and want to extend my knowledge regarding ANSI SQL.
I didn't wrote an exact schema yesterday just because my statement above.
Of course as I wrote it here (mysql section) because querying datetime columns this way in mysql is ok (based on my experience) the ANSI way would be using year and month functions (I think both very common across db platforms, even not ,I suppose, ANSI supported functions).
Optimizing queries through good indexes is a must, which I know so my questions are more theoretical.
Right now I got a new theoretical version of the schema which is the following:
(the n:m relations is between Routes and Fuels has been changed to 1:n relations, 1 Route contain 0 or more fuels taken)
Routes:
-> id
-> start_date
-> arrival_date
-> start_km
-> end_km
Couplings:
-> id
-> plate_number
Routes_Couplings: (Routes,Couplings join table, n:m relation):
-> route_id
-> coupling_id
Fuels: (1:n relation to Routes)
-> id
-> route_id
-> buy_date
-> amount
And my current theoretical query look like this:
PHP Code:
SELECT
cp.plate_number,
(
SELECT
SUM(f.amount)
FROM
Fuels f
WHERE
f.route_id = r.id
GROUP BY
f.route_id
) as fuelsum
,(r.end_km-r.start_km) as distance
FROM
Couplings cp
INNER JOIN Routes_Couplings rc ON rc.couple_id = cp.id
INNER JOIN Routes r ON rc.route_id = r.id
WHERE
YEAR(r.start_date) = 2011 AND MONTH(f.start_date) = 1
GROUP BY
cp.plate_number
I know this is still wrong as of a coupling can be part of more than one routes and a route can contain more couplings as well. But I keep working on it.
thanks
Sandor
sorry for my english if I was misleading in any way
