user3130996
user3130996

Reputation: 21

Count and display the most to less count in gridview

SELECT t1.CompanyName, COUNT(*) AS TotalOrder
FROM [table1] t1 
INNER JOIN [table2] t2 ON t1.CompanyID = t2.CompanyID
 GROUP BY t1.CompanyID
 ORDER BY COUNT(*) DESC

I have the above code already just that wish to display the most count To the top and less count at the bottom in the gridview how can I do this?

Upvotes: 1

Views: 55

Answers (2)

Raghubar
Raghubar

Reputation: 2788

Try This query.

SELECT t1.CompanyName, COUNT(t2.CompanyID) AS TotalOrder
FROM table1 t1 INNER JOIN table2 t2
     ON t1.CompanyID = t2.CompanyID
GROUP BY t1.CompanyName
ORDER BY TotalOrder DESC

Upvotes: 2

Kirk
Kirk

Reputation: 5077

As far as i understand your question is you want the Company Names listed with row number in descending order. if so try this

SELECT t1.CompanyName, ROW_NUMBER() over (order by CompanyName) AS TotalOrder
FROM table1 t1 INNER JOIN table2 t2
ON t1.CompanyID = t2.CompanyID order by CompanyName desc

Upvotes: 1

Related Questions