Have a problem with the below stored procedure.In a table of companynamesaccountcode, parent, nameZBOV0001 ZBOV0001 Big CompanyZCAT0001 ZBOV0001 Cat Company
The following stored procedure lists the name as Cat Company instead of the alphabetical Big Company.I cannot order the select max(companyname) name part because SQL won't allow me. ANy workarounds?--web_TopClientsAll 500, 0, 1CREATE PROCEDURE web_TopClientsAll (@topnum int,@month int,@sector int,@monthend int)ASSET ROWCOUNT @topnumSELECT Groups.CompanyName, Groups.ParentGroup, sum(Amounts.Turnover) AS Turnover, ClientSectors.Description, Groups.TAP, Groups.KAM, Groups.KADFROM(SELECT max(companyname) as companyname, parentgroup, ClientSector, TAP, KAM, KADfrom companies group by parentgroup, ClientSector, TAP, KAM, KAD ) Groups LEFT JOIN (SELECT C.ParentGroup, [amtin loccur] AS TurnoverFROM Companies CLEFT JOIN zarinvreg ON zarinvreg.Customer = C.AccountCodeWHERE monthdata <= @monthAND monthdata >= @monthend--@month-11 --this gives us 12 monthsAND C.CompanyName <> '-'AND C.CompanyName is not null) Amounts ON Groups.ParentGroup = Amounts.ParentGroupLEFT JOIN ClientSectors ON ClientSectors.ID = Groups.ClientSectorWHERE (ClientSectors.ID = @sector OR ISNULL(@sector ,'')='')AND Amounts.Turnover is not nullGROUP BY Groups.CompanyName, Groups.ParentGroup, ClientSectors.Description, Groups.TAP, Groups.KAM, Groups.KADORDER BY Amounts.Turnover descSET ROWCOUNT 0GO