简体   繁体   English

如何使用C#从ASP.NET中的SQL Server设置数据范围

[英]How to set ranges on data from SQL Server in ASP.NET with c#

I am creating a page on which there is a map of India. 我正在创建一个页面,上面有印度地图。 I have inserted data into the SQL Server database about economic indicators. 我已经将有关经济指标的数据插入到SQL Server数据库中。 I have also placed a map of India on the webpage. 我还在网页上放置了印度地图。

I want to be able to retrieve the data from the database and then get the greatest value from the data and set it as 100%. 我希望能够从数据库中检索数据,然后从数据中获取最大价值并将其设置为100%。 Then based on that if a value falls between 0 to 20% a that part of the map should be a certain color. 然后根据该值,如果值介于0到20%之间,则地图的该部分应为特定颜色。 If it falls between 20 to 40% it will be a different color and so on. 如果介于20%到40%之间,它将是不同的颜色,依此类推。

I am new to ASP.NET and SQL Server and not sure how to do this. 我是ASP.NET和SQL Server的新手,不确定如何执行此操作。 I really need help. 我真的需要帮助 Thank you in advance 先感谢您

Try something like this. 尝试这样的事情。

WITH cte 
     AS (SELECT states, 
                value, 
                ( value / NULLIF(Max(value) 
                                   OVER(), 0) ) * 100 AS [Percent] 
         FROM   yourtable) 
SELECT states, 
       value, 
       color = CASE 
                 WHEN [percent] BETWEEN 0 AND 20 THEN 'X color' 
                 WHEN [percent] BETWEEN 21 AND 40 THEN 'Y color' 
                 WHEN [percent] BETWEEN 41 AND 60 THEN 'X color' 
                 .....
               END 
FROM   cte 

Notes : 注意事项:

  • Formula used to calculate percentage (Value/max_value) * 100 . 用于计算百分比(Value/max_value) * 100公式。 Please correct if it is wrong. 如果错误,请更正。
  • Considering value as the column for which you need to find percentage value视为需要查找百分比的列
  • NULLIF is used to avoid divide by zero error NULLIF用于避免除以零错误
  • Max() Over() is to find the largest value in your table Max() Over()用于查找表中的最大值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 SQL Server 过程变量设置为从 C# ASP.NET MVC 程序输入的数据? - How to set SQL Server procedure variables to inputted data from C# ASP.NET MVC program? 如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008 - How to upload table data from Excel to SQL Server 2008 in C# ASP.Net 如何使用C#ASP.NET HttpClient将数据从Sql Server发送到另一个Sql Server(未链接) - How to send data from a Sql Server to another Sql Server (Not Linked) Using C# ASP.NET HttpClient 使用数据集进入SQL Server的新行[asp.net,c#] - New Row into SQL Server with Data Set [asp.net, c#] 在ASP.NET C#中将数据插入SQL Server数据库 - Inserting data to SQL Server database in asp.net c# 使用C#将动态数据插入asp.net中的sql服务器 - inserting dynamic data into a sql server in asp.net with C# ASP.NET C# 如何从 SQL Server 检索数据并将多行数据转换为逗号分隔的一行? - ASP.NET C# How to retrieve data from SQL Server and convert multi-row data into one row seperated by comma? 从SQL Server或asp.net/C#(mvc 3)页面将数据导入excel - importing data to excel from SQL Server or asp.net/C# (mvc 3) page 从C#/ ASP.NET应用程序将数据插入SQL Server - Insert data into SQL Server from a C# / ASP.NET application 使用 C# 从 ASP.NET 中的 SQL Server 检索数据 - Retrieving data from SQL Server in ASP.NET using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM