Calculate Hourly Energy Consumption - sql-server

I have a Table 'Meter1' in my MSSQL database DB_TEST. Table structure is like this,
MeterID
Timestamp
Value
7
2022-09-16 11:00:00.000
1800
7
2022-09-16 12:00:00.000
1805
7
2022-09-16 13:00:00.000
1820
7
2022-09-16 14:00:00.000
1860
7
2022-09-16 15:00:00.000
1875
I need to calculate the hourly consumption by substracting the current value - previous value.
I have achieved this by using LAG function,
SELECT [MeterID]
,[Timestamp]
,[Value]
,VALUE - LAG (VALUE)
OVER (ORDER BY TIMESTAMP) AS Consumption
FROM [DB_TEST].[dbo].[Meter1]
& the results like this
MeterID
Timestamp
Value
Consumption
7
2022-09-16 11:00:00.000
1800
NULL
7
2022-09-16 12:00:00.000
1805
5
7
2022-09-16 13:00:00.000
1820
15
7
2022-09-16 14:00:00.000
1860
40
7
2022-09-16 15:00:00.000
1875
15
But how can I update or Insert the same results to my existing table with another column "Consumption".

Related

How to compute a grouped, running calculation in SQL

I have records like below.
Employee DateTime Project HoursWorked
Ralph 10/19/16 7:00 A 5
Ralph 10/19/16 12:00 B 5
Ralph 10/19/16 17:00 C 5
Betty 10/19/16 8:00 A 9
Betty 10/20/16 9:00 A 6
Betty 10/20/16 15:00 B 3
I need to compute Regular Hours and OT Hours.For each employee, for each day, in datetime order, the first 8 hours are Regular Hours, then they are OT Hours.Note: Project does NOT come into use for this computation; it explains why there can be multiple time entries per day.
Desired output:
Employee DateTime Project HoursWorked RegHrs OTHrs
Ralph 10/19/16 7:00 A 5 5 0
Ralph 10/19/16 12:00 B 5 3 2
Ralph 10/19/16 17:00 C 5 0 5
Betty 10/19/16 8:00 A 9 8 1
Betty 10/20/16 9:00 A 6 6 0
Betty 10/20/16 15:00 B 3 2 1
How do I write SQL to do this? I am guesing that this will require a cursor, but I have no experience writing such.
Thank you

How to bulk update the date column

I have a table with 10000 records.
Sample
Id Transaction_Id Contract_Id Contractor_Id ServiceDetail_Id ServiceMonth UnitsDelivered CreateDate
----------------------------------------------------------------------------------------------------------------------------
1 1 352 466 590 2016-03-01 203 2016-04-25 17:01:55.000
2 1 352 466 566 2016-03-01 200 2016-04-25 17:02:38.807
3 1 352 466 138 2016-04-13 20 2016-04-13 00:00:00.000
5 1 352 466 138 2016-04-14 21 2016-04-13 00:00:00.000
6 10011 40 460 68 2016-03-17 10 2016-04-25 17:20:13.413
7 10011 40 460 511 2016-03-17 15 2016-04-25 17:20:13.413
8 10011 40 460 1611 2016-03-17 20 2016-04-25 17:20:13.413
9 20011 352 466 2563 2016-02-05 10 2016-04-25 17:20:25.307
11 100 40 460 68 2016-03-17 10 2016-04-25 17:29:23.653
In this table I have servicemonth with different dates.
I want to update the servicemonth column to the existing months last date.
suppose if I have 2016-03-17 in the table, it should be updated to 2016-3-31
suppose if I have 2016-05-12 in the table, it should be updated to 2016-5-31
Can anyone suggest a single query to update this?
EOMONTH: Returns the last day of the month that contains the specified date, with an optional offset.
UPDATE ... SET servicemonth = EOMONTH(servicemonth)
Update servicemonth
set servicemonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
--replace getdate() with the date column for which you want the end day of the month

How to get values between two dates with a interval of 10 minutes between the dates

SELECT
t0.brandID,
t0.brandName,
t0.cdt,
t0.udt,
t0.brandstatus,
(DATEPART(MINUTE, t0.cdt)) as tempt
FROM brands t0
WHERE
CONVERT(VARCHAR(10),t0.cdt,110) BETWEEN
CONVERT(VARCHAR(10),'01-11-2013',110) and CONVERT(VARCHAR(10),'11-11-2014',110)
The above query giving me values between the dates. I want to get values between these dates for every 10 minutes.
brandID brandName cdt udt brandstatus tempt
1 khasim 2013-11-01 19:14:18.123 2013-11-15 19:14:18.123 1 14
3 khasim 2013-11-02 19:17:57.700 2013-11-15 19:17:57.700 1 17
4 tanveer 2013-11-03 19:18:05.947 2013-11-15 19:18:05.947 1 18
5 abcdef 2013-11-04 20:50:06.783 2013-11-15 20:50:06.787 1 50
8 budwieser 2014-02-12 19:26:43.913 2014-02-12 19:26:43.913 1 26
expected result: (when selecting date between 15-11-2013 and 16-11-2013)
brandid brandname cdt udt
1 khasim 2013-11-15 (00:00:01)---first record created on that date
2 somethi 2013-11-15 (00:00:05)---second record
...
...
final record at (00:10:00) minutes...
need to calculate number of 10 minute intervals between selected date and for every 10 min interval need to get records.

get difference result between ef(dbfirst) and Sql Query

Before Start
I Have View 'PageViewModSession'
its code is
SELECT CONVERT(datetime, CONVERT(varchar(14), VisitStartDateTime) + ':00:00') AS DateValue, MAX(dbo.PageLogGroupByDateTimeFull(VisitStartDateTime)) AS PageLogCount,
(CASE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) WHEN 0 THEN 1 ELSE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) END) AS SessionLogCount, SiteInfoID
FROM dbo.PageLog
GROUP BY CONVERT(varchar(14), VisitStartDateTime), SiteInfoID
and When select this views my result is
and when select in ef with this syntaxt
var obj = db.PageViewModSessions.AsQueryable();
result is
repeat row one in every row on result
i catch created Sql query query in profiler
SELECT
[Extent1].[DateValue] AS [DateValue],
[Extent1].[PageLogCount] AS [PageLogCount],
[Extent1].[SessionLogCount] AS [SessionLogCount],
[Extent1].[SiteInfoID] AS [SiteInfoID]
FROM (SELECT
[PageViewModSession].[DateValue] AS [DateValue],
[PageViewModSession].[PageLogCount] AS [PageLogCount],
[PageViewModSession].[SessionLogCount] AS [SessionLogCount],
[PageViewModSession].[SiteInfoID] AS [SiteInfoID]
FROM [dbo].[PageViewModSession] AS [PageViewModSession]) AS [Extent1]
and result is
2015-11-03 01:00:00.000 19 9 2
2015-11-03 02:00:00.000 19 4 2
2015-11-03 03:00:00.000 4 1 2
2015-11-03 11:00:00.000 7 5 2
2015-11-03 12:00:00.000 9 2 2
2015-11-04 01:00:00.000 1 1 2
2015-11-04 02:00:00.000 12 1 2
2015-11-04 03:00:00.000 5 1 2
2015-11-04 05:00:00.000 1 1 2
2015-11-04 06:00:00.000 4 1 2
2015-11-04 10:00:00.000 20 2 2
2015-11-04 11:00:00.000 19 4 2
2015-11-04 12:00:00.000 23 18 2
2015-11-05 02:00:00.000 1 1 2
2015-11-05 03:00:00.000 5 1 2
2015-11-05 04:00:00.000 25 2 2
2015-11-05 10:00:00.000 2 1 2
2015-11-05 11:00:00.000 3 1 2
why ?!!
and what have to do fix this problem
I handle This
by Setting Two Key for model
DateValueand SiteInfoId

How to update record based on value of next record in SQL Server 2008

I like to update a column based on a value of the next record:
resid startdate enddate weekday hours
-----------------------------------------------
2980 2013-09-23 2014-12-31 1 6
2980 2013-09-23 2014-12-31 2 6
2980 2013-09-23 2014-12-31 3 6
2980 2013-09-23 2014-12-31 4 6
2980 2013-09-23 2014-12-31 5 6
2980 2015-01-01 NULL 1 6,8
2980 2015-01-01 NULL 2 6,8
2980 2015-01-01 NULL 3 6,8
2980 2015-01-01 NULL 4 6,8
2980 2015-01-01 NULL 5 6,8
2980 2015-07-01 NULL 1 6
2980 2015-07-01 NULL 2 6
2980 2015-07-01 NULL 3 6
2980 2015-07-01 NULL 4 6
2980 2015-07-01 NULL 5 6
I like to update the NULL value in column enddate. It needs to get a value of the next startdate - 1 day.
For instance, for all records with startdate 2015-01-01, the column enddate needs to be updated with 2015-07-01 - 1 day.
Is there someone who has a solution?
update TableName set enddate = dateadd(day,1,startdate)
One way to do it is with a subquery:
UPDATE t1
SET enddate = (SELECT TOP 1 DATEADD(DAY, -1, startdate)
FROM YourTable t2
WHERE t1.startdate < t2.startdate
ORDER BY t2.startdate)
FROM YourTable t1
WHERE t1.enddate is null

Resources