SQL group by month and add 15mins - sql

I have a table with data every 15mins.
I have this query :
Select Sum(Value) From MyTable Group By month(myDate), year(myDate)
However the result is not what i want because I have values from
'2013-01-01 00:00:00.000' to '2013-01-31 23:45:00.000' and I need to have values from
'2013-01-01 00:15:00.000' and '2013-02-01 00:00:00.000'
How can I change the request to have the correct date range ?
Thanks

Use DATEADD method:
SELECT Sum(Value)
FROM MyTable
GROUP BY MONTH(DATEADD(minute,15,myDate)), YEAR(DATEADD(minute,15,myDate))

Related

Convert Dates in SQL Server

I'm trying to perform a query in SQL Server. I'm having trouble filtering the date. The output is always the same, the data doesn't get filter by date.
The date comes in the following format:
29-12-2020 16:38
31-12-2020 17:43
I tried to filter doing all the following but none worked out:
select
start_date
from table_1
where start_date between '01-01-2021 00:00:00' and '31-01-2021 00:00:00'
select
start_date
from table_1
where start_date between '01-01-2021 00:00' and '31-01-2021 00:00'
select
start_date
from table_1
where start_date between '01-01-2021' and '31-01-2021'
I also tried to cast it but I didn't have luck:
select
cast(start_date as date) as start_date
from table_1
where start_date between '2021-01-01' and '2021-01-31'
Can anyone help me?
Regards
You have a string. I strongly suggest making it a date/time of some sort. The following conversion works:
select convert(datetime, left(str, 10), 105) + convert(datetime, right(str, 5))
from (values ('29-12-2020 16:38')) v(str);
One operation is a computed column:
alter table table_1 start_date_dt as
( convert(datetime, left(start_date, 10), 105) + convert(datetime, right(start_date, 5)) )
Then you can use this value for your where clause. Or better yet. Fix the data! Don't store values in strings when there is an appropriate data type.

How can I get the updated record in 30 days till today?

I have a small question about SQL Server: how to get the last 30 days information from this column from table1:
created_at
updated_at
2020-02-05T01:25:42Z
2020-02-05T01:25:42Z
2020-05-05T02:31:56Z
2020-05-05T02:31:56Z
With the above data, I would need something like day count within 30 days.
I have tried
SELECT * FROM table1
DATEDIFF(CAST(SUBSTR(updated_at,1,10)) AS VARCHAR,CURRENT_TIMESTAMP) BETWEEN 0 AND 30 ;
and
SELECT * FROM table1
WHERE updated_at BETWEEN DATETIME('now', '-30 days') AND DATETIME('now', 'localtime')
Would need your expertise to help me with this query
Thank you!
SELECT *
FROM (
SELECT otherColumns
, DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_at) AS updated_at
FROM table1
) b
WHERE CAST(b.updated_at AS DATE) >= DATEADD(DAY,-30,GETDATE())
I think this will help you
If you want a count of updates by day for 30 (or so) days, then:
SELECT CONVERT(DATE, updated_at) as dte, COUNT(*)
FROM table1
WHERE updated_at >= DATEADD(DAY, -30, CONVERT(DATE, GETDATE()))
GROUP BY CONVERT(DATE, updated_at)
ORDER BY CONVERT(DATE, updated_at);
Note that SQLite date/time functions (which your code uses) are very peculiar to to SQLite. So are SQL Server's -- although I personally find them easier to remember.

change select statement value using sql

I have some SQL below which is programmatically generated:
INSERT INTO TABLE (COMID, NAME, DATE)
SELECT DISTINCT 'COM001', 'John', '01-Jan-4501 00:00:00'
How can i amend this so that if it finds the date time of : 01-Jan-4501 00:00:00 then it replaces it with todays date?
You probably want to avoid some wrong or irrelevant values.
SELECT
'COM001',
'John',
case
when cast('01-Jan-4501 00:00:00' as date) > cast('01-Jan-2100 00:00:00' as date)
then cast(getdate() as varchar(30))
else '01-Jan-4501 00:00:00'
end
Another way is to create a trigger on that table that automatically makes the date field correction.
SELECT DISTINCT 'COM001', 'John',
case when date = '01-Jan-4501 00:00:00' then convert(date,getdate()) end

SQL Server 2005 extract a date from a string

I have a view I am creating and I want to group by an extracted date from a column that has a date and time. This is where I am....
SELECT
ClockCode AS MOnum, SUM(Actual_Hrs) AS Runtothours, TStamp
FROM
dbo.Raw_Booking
WHERE
(Actual_Hrs > 0) AND (ClockCode LIKE 'MO%')
AND (TStamp > CONVERT(DATETIME, '2013-01-01 00:00:00', 102))
GROUP BY
ClockCode, TStamp
so while I have it grouped by the TStamp column there is a record for each one based on the time... I am looking to get a total amount of run time for each order by date.
The TStamp column is formatted as:
2013-01-02 08:18:47.000
If you can change your schema, store it as a date to begin with. Otherwise, change your group by clause to something similar to...
GROUP BY CAST(TStamp AS DATE)
EDIT: Forgot you were in SQL 2005. You can still do something like the following, but keep in mind this should be considered an ugly, short-term, workaround. All the comments in this thread about redesigning are completely valid and should be pursued...
GROUP BY SUBSTRING(TStamp, 0, 11)
SELECT ClockCode AS MOnum
, SUM(Actual_Hrs) AS Runtothours
, TStampDate
FROM dbo.Raw_Booking
CROSS APPLY (
SELECT CONVERT(datetime, TStamp, 102) AS TStampDateTime
) AS CA1
CROSS APPLY (
SELECT DATEADD(day, DATEDIFF(day, 0, TStampDateTime), 0) AS TStampDate
) AS CA2
WHERE Actual_Hrs > 0
AND ClockCode LIKE 'MO%'
AND TStampDateTime > CONVERT(DATETIME, '2013-01-01 00:00:00', 102))
GROUP BY ClockCode
,TStampDate

How can I group by arbitary time period with SQL

This is similar but not equal to my previous question
That was about how to summarize log-items per day.
I use this SQL.
SELECT
[DateLog] = CONVERT(DATE, LogDate),
[Sum] = COUNT(*)
FROM PerfRow
GROUP BY CONVERT(DATE, LogDate)
ORDER BY [DateLog];
Now I want to improve that to summarize over an arbitary time period.
So instead of sum per day, sum per hour or 5 minutes.
Is this possible ?
I use SQL Server 2008 R2
You can round LogDate using DATEADD and DATEPART and then group by that.
Example (groups by five second intervals):
SELECT
[DateLog] = DATEADD(ms,((DATEPART(ss, LogDate)/5)*5000)-(DATEPART(ss, LogDate)*1000)-DATEPART(ms, LogDate), LogDate),
[Sum] = COUNT(*)
FROM
(
SELECT LogDate = '2013-01-01 00:00:00' UNION ALL
SELECT LogDate = '2013-01-01 00:00:04' UNION ALL
SELECT LogDate = '2013-01-01 00:00:06' UNION ALL
SELECT LogDate = '2013-01-01 00:00:08' UNION ALL
SELECT LogDate = '2013-01-01 00:00:10'
) a
GROUP BY DATEADD(ms,((DATEPART(ss, LogDate)/5)*5000)-(DATEPART(ss, LogDate)*1000)-DATEPART(ms, LogDate), LogDate)