After a long headache, the problem is finally solved today. Thanks to an expert on MSN.
Here, I want to help my friends who encounter the same problem,
If you have any suggestions for better solutions.
The problem is as follows:
Pre-calculate the hourly power usage of electric meters on each floor. The calculation method is the maximum value in the hour minus the maximum value in the previous hour under the same conditions, if there is no record for the previous hour, the hour is not displayed.
Solution:
The SQL statement is as follows:
Select fhub as build, fnumber as floor, {fn hour (ftime)} as hours,
Convert (char (10), ftime, 120) as days, max (fvalue) as Value
From DBO. [value]
Group by {fn hour (ftime)}, convert (char (10), ftime, 120), fhub, fnumber
It is recommended that you do not have statements such as order by sorting, which will be a waste of time. I first wrote the sorting to better observe the data and forgot to delete it. In the end, the running speed is always slow, and the problem cannot be found. Program Find the reason, depressed for a long time, later, the students looked at it and said that there is a sort to find, alas, sadly.
Next,
Select a. Days, A. Hours, A. Build, A. Floor, A. Value-isnull
(Select B. value from Haha B where B. build =. build and B. floor =. floor and B. hours =. hours-1 and B. days =. days), (select B. value from Haha B where B. build =. build and B. floor =. floor and B. hours = 23 and B. days = dateadd (day,-1,. days) as Value
From Haha A where build = 'b4 'and floor = 3 and days = '2017-09-11'
Use a subquery to subtract the fields with the difference. It is best to write the last where statement or replace the parameter in the stored procedure. Without the following conditions, SQL Server calculates all the data. I used my data for a test and it took about two minutes to complete the experiment. What if a statement in the query analyzer does not run in five seconds?
In short, a small error in SQL statements may make your program very bad. I am only a little bit of experience here. I hope I can help you a little bit, and I also hope you can give more comments.