简体   繁体   中英

C# calculate difference from two rows based on a sql query

I have a task to solve. I am trying to display the operation time of two machines (number1 & number 2) in a diagram. Therefore i store information in a table. The columns are id, date, number1, number2. Lets assume i have this specific dataset:

 id date      number1    number2

  1| 24.09.14 | 100   |    120

  2| 01.10.14 | 150   |     160

For displaying the information I need to retrieve the following data.

((number1(2)- number1(1)) + number2(2) - number1(1))/2)/(number of days (date2 - date1))

This should result in the following specific numbers.

((150-100 + 160-120)/2)/7= 6,42

Or in plain words. The result should be the average daily operation time from all of my machines. Substracting saturdays and sundays from the number of dates would be nice but not necessary.

I hope that you understand my question. In essence I am facing the problem that i dont know how to work with different rows from a simple sql query. The programming language is c# in a razor based web project.

Yes you can do it with sql query. Try the below query.

SELECT 
       N1.Date as PeriodStartDate,
       N2.Date as PeriodEndDate,
       CAST(CAST((((N2.number1- N1.number1) + (n2.number2 - N1.number2))/2) AS DECIMAL(18,2))/(datediff(d,n1.date,n2.date)) AS DECIMAL(18,2) ) AS AverageDailyOperation
FROM 
[dbo].[NumberTable] N1 
INNER JOIN  
[dbo].[NumberTable] N2 
ON  N2.Date>N1.Date

I have assumed the table name as NumberTable, I have added PeriodStartDate and PeriodEndDate to make it meaningful. You can remove it as per your need.

Your formule have probably a mistake in front of your numerical sample :
((number1(2)- number1(1)) + number2(2) - number (1))/2)/(number of days (date2 - date1)) (2)-数字 (1))/ 2)/(天数(日期2-日期1))

If the values ​​of the id column are chronological and have no holes (1.2, 3, 4, ... OK but 1,3,4, 6 KO ...) you can try the following script :

  

--- I create a #tmp table for test
CREATE table #tmp 
(
 id int,
 Date DateTime,
 number1 float, 
 number2 float
 )

--- insert samples data
 INSERT INTO #tmp (id, Date, number1, number2) VALUES (1, '2014-09-24T00:00:00', 100, 120), (2, '2014-10-01T00:00:00', 150, 160)

it work great on my SQL Server

First I doubt that you have only 2 records in database. Here some code that makes calculation for every 2 rows in DataSet.

for(int i=0; i < dst.Tables[0].Rows.Count - 1; i+=2)
{
    if(dst.Tables[0].Rows.Count % 2 != 0)
         Console.WriteLine("Wrong records count")

    int number1Row1 =Convert.ToInt32(dst.Tables[0].Rows[i]["Number1"]);
    int number1Row2 =Convert.ToInt32(dst.Tables[0].Rows[i]["Number2"]);
    int number2Row1 =Convert.ToInt32(dst.Tables[0].Rows[i+1]["Number1"]);
    int number2Row2 =Convert.ToInt32(dst.Tables[0].Rows[i+1]["Number2"]);

    DateTime dateRow1 =Convert.ToDateTime(dst.Tables[0].Rows[i]["Date"]);
    DateTime dateRow2 =Convert.ToDateTime(dst.Tables[0].Rows[i+1]["Date"]);

    double calc = ((number1Row2- number1Row1 + number2Row2 - number2Row1)/2)*(dateRow1 - dateRow2).TotalDays

    Console.WriteLine(calc);
}

It is wroted to be maximum clear to understand.

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