Select (Display) records in ascending order in SQL Server

elvisidrizi1
 
on Feb 12, 2021 06:37 AM
456 Views

Hi Everyone,

I have this code that I use for Vb.net and it used to work fine but now it shows results not in ascending way.

This is the code :

AS
Begin
   SELECT
        Sum(Price) As Total,
        DATEPART(yyyy, Date) As [year],
	Sum (CONVERT(INT, CONVERT(VARCHAR(MAX),quantity))) as Lesh	
    FROM
        dbo.Expenses
    WHERE
	 Date >= DATEADD(year, -12, GETDATE()) 
    GROUP BY
    DATEPART(yyyy, Date)
End

and these is the results

7566.53 2021 163

187.20 2016 13

17346.86 2019 1135

18594.50 2020 1933

13760.48 2017 224

12975.71 2018 277 

I want to have the results like:

2021

2020

2019

2018

2017

2016

Thank you, Best regards

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 12, 2021 06:38 AM

Hi elvisidrizi1,

Add order by statement.

SELECT
    Sum(Price) As Total,
    DATEPART(yyyy, Date) As [year],
    Sum (CONVERT(INT, CONVERT(VARCHAR(MAX),quantity))) as Lesh         
FROM
    dbo.Expenses
WHERE
    Date >= DATEADD(year, -12, GETDATE())
GROUP BY
DATEPART(yyyy, Date)
ORDER BY DATEPART(yyyy, Date) ASC