shahab Posted June 8, 2004 Posted June 8, 2004 How can I get the percentage of each field? http://shahabedeen.europe.webmatrixhosting.net/pic/persentile04.gif Quote
JABE Posted June 8, 2004 Posted June 8, 2004 Try this: SELECT rank/ (select cast(sum(rank) as float) from tblpoll) as rank from tblpoll Quote
Administrators PlausiblyDamp Posted June 8, 2004 Administrators Posted June 8, 2004 something like SELECT rank, (cast (rank as float) * 100 / (select sum(rank) from tblpoll)) from tblpoll without knowing the table structure for sure I can't be sure. However here is a similar idea using northwind's Employees table select reportsto,(select sum(reportsto) from employees), (cast (reportsto as float) * 100 / (select sum(reportsto) from employees)) from employees Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
shahab Posted June 9, 2004 Author Posted June 9, 2004 Dear guy,I have a new problem: http://shahabedeen.europe.webmatrixhosting.net/pic/persentile05.gif As u can see the float point is not beautiful enough,How can I reduce the float points to 2 number? :rolleyes: :D Quote
JABE Posted June 10, 2004 Posted June 10, 2004 Try this: SELECT CAST(rank/ (select cast(sum(rank) as float) from tblpoll) AS NUMERIC(5,2)) as rank from tblpoll Quote
shahab Posted June 13, 2004 Author Posted June 13, 2004 ANOTHER PROBLEM: "SELECT RName,Rank,"+ "(cast (rank as float) * 100/"+ "(SELECT CAST(sum(rank) AS FLOAT )from tblpoll AS NUMERIC(3,2) )"+ " as Percentage FROM tblPoll)"; //SELECT CAST(rank/(select cast(sum(rank) //as float) from tblpoll) AS NUMERIC(5,2)) as rank from tblpoll error: Line 1: Incorrect syntax near ','. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near ','. Source Error: Line 100: Line 101: SqlDataReader myReader; Line 102: myReader = myCommand.ExecuteReader(); Line 103: Line 104: DataGrid1.DataSource = myReader; Source File: d:\inetpub\wwwroot\projects\poll\pooluc.ascx.cs Line: 102 Quote
Administrators PlausiblyDamp Posted June 13, 2004 Administrators Posted June 13, 2004 Could you also post the line of code wher eyou are building the command string along with the variable declarations. If you do a Debug.WriteLine () what does it lookl like? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
shahab Posted June 14, 2004 Author Posted June 14, 2004 (edited) void GridBinder() { string strCon = ConfigurationSettings.AppSettings["ConnStr"]; SqlConnection objConn = new SqlConnection(strCon); string mySelectQuery = "SELECT Rname,rank,CAST(rank/"+ "(select cast(sum(rank) as float) from tblpoll) AS NUMERIC(5,2)) as Percentage from tblpoll"; /*"SELECT RName,Rank,"+ "(cast (rank as float) * 100/"+ "(SELECT CAST(sum(rank) AS FLOAT )from tblpoll AS NUMERIC(3,2) )"+ " as Percentage FROM tblPoll)"; */ //SELECT CAST(rank/(select cast(sum(rank) //as float) from tblpoll) AS NUMERIC(5,2)) as rank from tblpoll SqlCommand myCommand = new SqlCommand(mySelectQuery,objConn); objConn.Open(); SqlDataReader myReader; myReader = myCommand.ExecuteReader(); DataGrid1.DataSource = myReader; DataGrid1.DataBind(); } Edited November 26, 2005 by PlausiblyDamp Quote
JABE Posted June 15, 2004 Posted June 15, 2004 Try this: SELECT RNAME, RANK, CAST(rank/(select cast(sum(rank) as float) from tblpoll) * 100 AS DECIMAL(5,2)) as Percentage from tblpoll Quote
shahab Posted June 15, 2004 Author Posted June 15, 2004 Correct ... Tnx alot guy.U 're really a good man Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.