简体   繁体   中英

How to calculate percentage figure from local database

I am currently trying to work out the percent remaining within a budget. The application I created is a budgeting application. The two columns I will be using to work out the calculation is BudgetAmount (this is the current balance of budget) and TransactionAmount (this is used to input expenses and income into the budget). I am unsure how to save the results from the sql query to a decimal value so I can work out percentage. Any help would be appreciated, thank you in advance.

(TransactionType = 2) - This means this type of transaction was an expense

This is the two sql statements for obtaining the data I think?

SqlCommand percentage = new SqlCommand("select * from UserBudget where budgetID=@budgetid", conn);

SqlCommand percentage2 = new SqlCommand("select * from userbudgettransaction where budgetID=@budgetid AND TransactionType = 2", conn);

Below is the database columns I am using:

UserBudget UserBudget UserBudgetTransaction

Here's an example with some sample data: note I have multiplied the division by 100 to get the actual percentage.


 create table UserBudget (BudgetId int, BudgetAmount varchar(50))
 create table UserBudgetTransaction (UserBudgetTransactionId int, BudgetId int, TransactionAmount decimal(18, 2), TransactionType int)
 insert into UserBudget (BudgetId, BudgetAmount) values (1, '100000')
 insert into UserBudget (BudgetId, BudgetAmount) values (2, '200000')

 insert into UserBudgetTransaction (UserBudgetTransactionId, BudgetId, TransactionAmount, TransactionType ) values (3, 1, '1000', 1)
 insert into UserBudgetTransaction (UserBudgetTransactionId, BudgetId, TransactionAmount, TransactionType ) values (4, 1, '20000', 2)
 insert into UserBudgetTransaction (UserBudgetTransactionId, BudgetId, TransactionAmount, TransactionType) values (5, 1, '50000', 2)

 declare @budgetId int = 1

 select * from UserBudget
 select * from UserBudgetTransaction

 SELECT (SUM(TransactionAmount) / BudgetAmount) * 100 as Percentage 
 FROM UserBudget JOIN UserBudgetTransaction ON UserBudget.BudgetID = UserBudgetTransaction.BudgetID 
 WHERE UserBudget.BudgetID = @budgetid AND TransactionType = 2 
 group by UserBudget.BudgetID, BudgetAmount

This returns these values:

在此处输入图片说明

And in code:

var sql = @"SELECT (SUM(TransactionAmount) / BudgetAmount) * 100 as Percentage 
 FROM UserBudget JOIN UserBudgetTransaction ON UserBudget.BudgetID = UserBudgetTransaction.BudgetID
 WHERE UserBudget.BudgetID = @budgetid AND TransactionType = 2
 group by UserBudget.BudgetID, BudgetAmount";

 decimal result;

 using (var conn = new SqlConnection("Server=localhost;Database=Budgets;Trusted_Connection=True"))
 {
    using (var cmd = new SqlCommand(sql, conn))
    {
        cmd.CommandType = CommandType.Text;
        var parameter = cmd.Parameters.Add("budgetId", SqlDbType.Int);
        parameter.Value = 1;
        conn.Open();

        result = (decimal)cmd.ExecuteScalar();      
    }
 }

 Console.WriteLine(result);

Which returns:

在此处输入图片说明

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