"Sonnich" <so************@elektrobit.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I am making an app which has to work on a number of DB systems. This
requires simple SQL which will work almost everywhere.
Something like this does not work in MS access
select b.name,a.code,count(a.code) as x
from a, b
where....
group by code
order by x
So I have to do this
--- same--..
group by code,name
order by count(a.code)
Or is there a better solution?
I wonder hot the 2nd count is translated, e.g. when I have large tables
and different systems, it will run it 2 times, and become slower.
Sonnich
The first has "group by code" while the second has "group by code,name".
Could it be that you did not mean to write this and in fact you question is
can I sort on an alias column?
The short answer is no, but you could do the following to query the sample
Northwind database to get a list of customers with those that had placed the
most orders at the top of the list. Here we use the column number in the
order by clause.
Original:
SELECT Customers.CustomerID, COUNT(*) AS Qty
FROM Customers INNER JOIN Orders ON
Customers.CustomerID=Orders.CustomerID
GROUP BY Customers.CustomerID
ORDER BY COUNT(*) DESC
Changed to:
SELECT Customers.CustomerID, COUNT(*) AS Qty
FROM Customers INNER JOIN Orders ON
Customers.CustomerID=Orders.CustomerID
GROUP BY Customers.CustomerID
ORDER BY 2 DESC
As to how much any application might slow down, I think you have to just try
it out for yourself with sample data. Even if it did double the time (which
I'm not sure it would), you also need to look at the absolute time - e.g.
another 0.3 seconds may be of no consequence. In cases where the speed of a
query is an absolute must, and you have no other choice you can break the
normalisation rules and store a calculated column as an indexed field in the
table. Of course, that brings its own issues with it, but nothing will beat
it for speed.