Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am currently having an issue on the following query.

The query runs fine and returns the correct data but my order by is messed up

it returns
0-50
101-200
201-300
301-500
1000
51-100

any idea why this could happen

this is my query



SQL
select
                case when amt >=0 and amt <50.00
                        then ' R0-R50'
                    when amt >=51.00 and amt <100.00
                        then 'R51-R100'
                    when amt >=101 and amt <200.00
                        then ' R101-R200'
                    when amt >=201 and amt <300.00
                        then ' R201-R300'
                    When amt >=301 and amt<501.00
                        then ' R301-R500'
                    When amt >=501 and amt < 1000.00
                        then ' R501-1000'
                            When amt >=1000
                        then '1000 and above'

                end [Range], count(*) [Number], sum(amt) [Total], round(avg(amt),2) [Average]
            from trxtable
            where status='Processed'
                and Program_ID = @ProgramID
                and amt > 0
                AND trxtable.Cd IN (SELECT Cd from Tblmem WHERE isnull(Comments, '') NOT LIKE 'TESTCARD')
                AND (CONVERT(CHAR(10),trxdate  ,120)) >= @startDate AND (CONVERT(CHAR(10),trxdate  ,120)) <= @endDate
            group by
                case when amt >=0 and amt <50.00
                        then ' R0-R50'
                    when amt >=51.00 and amt <100.00
                        then 'R51-R100'
                    when amt >=101 and amt <200.00
                        then ' R101-R200'
                    when amt >=201 and amt <300.00
                        then ' R201-R300'
                    When amt >=301 and amt<501.00
                        then ' R301-R500'
                    When amt >=501 and amt < 1000.00
                        then ' R501-1000'
                        When amt >=1000
                        then '1000 and above'
                end
            order by
                case when amt >=0 and amt <50.00
                        then ' R0-R50'
                    when amt >=51.00 and amt <100.00
                        then 'R51-R100'
                    when amt >=101 and amt <200.00
                        then ' R101-R200'
                    when amt >=201 and amt <300.00
                        then ' R201-R300'
                    When amt >=301 and amt<501.00
                        then ' R301-R500'
                    When amt >=501 and amt < 1000.00
                        then ' R501-1000'
                        When amt >=1000
                        then '1000 and above'
            end
    END

    SELECT @ErrorCode = 001
    SELECT @Errormessage='Successful'
END



GO
Posted

Change the Order By Clause to
C#
ORDER BY Amt
 
Share this answer
 
Change the Order By clause to include a value that will cause the sort order you desire.

SQL
order by
    case when amt >=0 and amt <50.00
            then 1
        when amt >=51.00 and amt <100.00
            then 2
        when amt >=101 and amt <200.00
            then 3
        when amt >=201 and amt <300.00
            then 4
        When amt >=301 and amt<501.00
            then 5
        When amt >=501 and amt < 1000.00
            then 6
        When amt >=1000
            then 7
end


You might need to change the Group By, too
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900