简体   繁体   中英

How to display label based on maximum count in database? ASP.NET C#

I currently have a table called Order_Details. Here, all records are stored like the example shown below:

    OrderID   Product_Name
    1            Alpha
    2            Alpha
    3            Alpha
    4            Bravo
    5            Charlie

I have used the following sql statement to determine which record occurs the most:

select top 1 Product_Name, count(*) from [Order_Details]
group by Product_Name
order by count(*) desc

Output is below:

 Product_Name    (No column name)
        Alpha         10

Right now, I would like to enable a Label called "Best Sellers" to appear after executing the query above. The label is to only appear for Product_Name that has the highest count.

My question now is how to use the following SQL statement to check the values from the database and enable the label to appear.

select top 1 Product_Name as 'Best Sellers', count(*) as 'Sales' from [Order_Details]
group by Product_Name
order by count(*) desc
select top 1 Product_Name, count(*) as cnt, 'Best Seller' as label1 from [Order_Details]
group by Product_Name
order by count(*) desc

See the fiddle here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM