Subtraction for each complete month for real time database querying

一曲冷凌霜 提交于 2019-12-11 17:00:04

问题


I have a question, a bit different of Apply a substracton for each month

About these SQL :

;WITH cte AS
(
    SELECT DISTINCT
        Annees = YEAR(DateTime),
        Mois = MONTH(DateTime),
        firstRecord = first_value(value) OVER (PARTITION BY YEAR(DateTime), MONTH(DateTime) ORDER BY DateTime ASC),
        lastRecord = first_value(value) OVER (PARTITION BY YEAR(DateTime), MONTH(DateTime) ORDER BY DateTime DESC)
    FROM 
        AnalogHistory 
    WHERE
        TagName = 'A_000000000000000000000000000058.PV_Kw'
        AND DateTime >= '01/01/2016 00:00:00' 
        AND DateTime <= '24/07/2017 13:53:31'
)
SELECT
    Annees, Mois, 
    value = isnull(lastRecord, 0) - isnull(firstRecord, 0)
FROM
    cte


SELECT 
    YEAR(DateTime) AS Annee, MONTH(DateTime) AS Mois,
    MIN(Value) As MinVal, MAX(Value) As MaxVal,
    MAX(Value) - MIN(Value) As Difference
FROM 
    AnalogHistory 
WHERE
    TagName = 'A_000000000000000000000000000058.PV_Kw' AND
    DateTime >= '01/01/2016 00:00:00' AND
    DateTime <= '24/07/2017 13:53:31'
GROUP BY 
    YEAR(DateTime), MONTH(DateTime)
ORDER BY 
    YEAR(DateTime), MONTH(DateTime)

They are all good for what I'm asking for. But for now, it's a bit different.

As I said, I have a historian database, and this kind of query will return 100 rows, spaced by the same interval, so for the query, I will get:

2017-03-31 07:39:04.1310000
2017-04-06 01:57:47.4740000
2017-04-11 20:16:30.8180000
...

This is problematic because the value for 2017-04-06 01:57:47.4740000 might not be the same as the value the 2017-04-01 00:00:00.0000000, and the result the output is false.

So basically, I'll need to take the same query, but applying it for the very first moment of a month and the very last moment of a month.

Any ideas?

EDIT

I have to select for each month the first moment and the last moment, and with these, i need to query my database to get the corresponding value and substract it.

So what i want is getting value for :

2017-03-01 00:00:00.0000000 | 12
2017-03-31 23:59:59.9999999 | 24
2017-04-01 00:00:00.0000000 | 24
2017-04-30 23:59:59.9999999 | 78
...

instead of :

2017-03-31 07:39:04.1310000 | 23
2017-04-06 01:57:47.4740000 | 38
2017-04-11 20:16:30.8180000 | 42
...

The historian database will be able to retrieve me value as well, so don't have to worry about having data for the first and last moment of month

With these value, i need to substract it and retrieve it, so this might be :

Year | Month | Value
2017 | 03    | 12
2017 | 04    | 54
...

It's kind of difficult to explain well, but the database i'm trying to querying is using SQL as an interface to read the data. In fact, the real database in time based, so i will store each value of a object among he will get value. So i can query at any moment, the database will be able to retrieve me something.

Here is an exemple of output :

I'm not able to retrieve anything else about the table design. All i know is that the TagName condition is mandatory.

来源:https://stackoverflow.com/questions/45296257/subtraction-for-each-complete-month-for-real-time-database-querying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!