I have been struggling to show only the top 10 results in a grid application. I created my own SQL statement that returns the top 10 totals descending:
SELECT
cl.clName,
sum(mi.mntTotalCharge) as total ,
mi.mntMonth,
mi.mntYear
FROM
Clients cl INNER JOIN Agreements agr ON cl.cID = agr.cID
INNER JOIN MonthlyInvoices mi ON agr.agrID = mi.agrID
where mi.mntMonth = Month(now()) and mi.mntYear =year(now())
Group by cl.clName,mi.mntYear,mi.mntMonth
order by total DESC
limit 10
With reference to :
http://www.scriptcase.net/blog/mysql-limit-query/
and
http://www.scriptcase.net/forum/foru…id-application
How can this be done ?
Scriptcase changes the query to :
[SIZE=12px]SELECT clName, mntMonth, mntYear, total from
(SELECT cl.clName, sum(mi.mntTotalCharge) as total , mi.mntMonth, mi.mntYear
FROM Clients cl INNER JOIN Agreements agr ON cl.cID = agr.cID INNER JOIN MonthlyInvoices mi ON agr.agrID = mi.agrID where mi.mntMonth = Month(now()) and mi.mntYear =year(now())
Group by cl.clName,mi.mntYear,mi.mntMonth limit 10) nm_sel_esp [/SIZE][SIZE=12px]order by total DESC[/SIZE]
[SIZE=12px]This returns 10 records, but the first 10 alphabetically not the top 10 totals.
The query should be: [/SIZE]
[SIZE=12px]SELECT clName, mntMonth, mntYear, total from
(SELECT cl.clName, sum(mi.mntTotalCharge) as total , mi.mntMonth, mi.mntYear
FROM Clients cl INNER JOIN Agreements agr ON cl.cID = agr.cID INNER JOIN MonthlyInvoices mi ON agr.agrID = mi.agrID where mi.mntMonth = Month(now()) and mi.mntYear =year(now())
Group by cl.clName,mi.mntYear,mi.mntMonth [/SIZE]order by total DESC[SIZE=12px] limit 10) nm_sel_esp [/SIZE]
I have disabled sorting of the grid.
Is this a bug or am I missing something ?
Update I have created a view as a workaround but this is not the most elegant solution…