SQL Difference Calculation

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.