converting time in sql - sql

i am trying to get getting Between 15 minutes and 1 hour. the below SQL is what I have come up with using TIMESTAMPDIFF. How i am getting an error of 'TIMESTAMPDIFF' is not a recongnized built-in function name.
My SQL
SELECT Name, count(*)
FROM [test.database]
where TME between '2018-10-01 00:00:00.000' and '2019-01-31 00:00:00.999'
and TIMESTAMPDIFF(SECOND, date_trunc('SECOND', DT), date_trunc('SECOND', TME)) >= 900
and TIMESTAMPDIFF(SECOND, date_trunc('SECOND', DT), date_trunc('SECOND', TME)) < 3600
group by Name
order by Name
could someone help me to make my SQL work please.
thankS

Presumably, you are using SQL Server and want datediff(). However, I would use dateadd().
The initial date comparisons look quite cumbersome. I don't see why you would want the ending to be up to one second into Jan 31. So I'm guessing you want something like this:
select Name, count(*)
from [test.database]
where TME >= '2018-10-01' and
TME < '2019-01-31' and
TME >= dateadd(SECOND, 900, DT) and
TME <= dateadd(SECOND, 3, DT)
group by Name
order by Name

SELECT Name, count(*)
FROM [test.database]
where (TME between '2018-10-01 00:00:00.000' and '2019-01-31 00:00:00.999')
and (datepart(SECOND,TME) between 15 and 60)
group by Name
order by Name

Related

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.

Redshift: Running query using GETDATE() at specified list of times

So, I have a query that uses GETDATE() in WHERE and HAVING clauses:
SELECT GETDATE(), COUNT(*) FROM (
SELECT 1 FROM events
WHERE (event_time > (GETDATE() - interval '25 hours'))
GROUP BY id
HAVING MAX(event_time) BETWEEN (GETDATE() - interval '25 hours') AND (GETDATE() - interval '24 hours')
)
I'm basically trying to find the number of unique ids that have their latest event_time between 25 and 24 hours ago with respect to the current time.
The problem: I have another table query_dts which contains one column containing timestamps. Instead of running the above query on the current time, using GETDATE(), I need to run in on the timestamp of every entry of the query_dts table. Any ideas?
Note: I'm not really storing query_dts anywhere. I've created it like this:
WITH query_dts AS (
SELECT (
DATEADD(hour,-(row_number() over (order by true)), getdate())
) as n
FROM events LIMIT 48
),
which I got from here
How about avoiding the generator altogether and instead just splitting the intervals:
SELECT
dateadd(hour, -distance, getdate()),
count(0) AS event_count
FROM (
SELECT
id,
datediff(hour, max(event_time), getdate()) AS distance
FROM events
WHERE event_time > getdate() - INTERVAL '2 days'
GROUP BY id) AS events_with_distance
GROUP BY distance;
You can use a JOIN to combine the two queries. Then you just need to substitute the values for your date expression. I think this is the logic:
WITH query_dts AS (
SELECT DATEADD(hour, -(row_number() over (order by true)), getdate()) as n
FROM events
LIMIT 48
)
SELECT d.n, COUNT(*)
FROM (SELECT d.n
FROM events e JOIN
query_dts d
WHERE e.event_time > d.n
GROUP BY id
HAVING MAX(event_time) BETWEEN n - interval '25 hours' AND n
) i;
Here's what I ended up doing:
WITH max_time_table AS
(
SELECT id, max(event_time) AS max_time
FROM events
WHERE (event_time > GETDATE() - interval '74 hours')
GROUP BY id
),
query_dts AS
(
SELECT (DATEADD(hour,-(row_number() over (ORDER BY TRUE) - 1), getdate()) ) AS n
FROM events LIMIT 48
)
SELECT query_dts.n, COUNT(*)
FROM max_time_table JOIN query_dts
ON max_time_table.max_time BETWEEN (query_dts.n - interval '25 hours') AND (query_dts.n - interval '24 hours')
GROUP BY query_dts.n
ORDER BY query_dts.n DESC
Here, I selected 74 hours because I wanted 48 hours ago + 25 hours ago = 73 hours ago.
The problem is that this isn't a general-purpose way of doing this. It's a highly specific solution for this particular problem. Can someone think of a more general way of running a query dependent on GETDATE() using a column of dates in another table?

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 to select using where clause?

I have two dates, From date and To Date.
Also i have two time fields, From Time and To Time.
The date field in the database is Datetime. I need to select data according to both date and time.
This is my query for selecting data between 13:00 to 15:00, but it is not suitable for 20:00 to 08:00.
where Date>= '2/01/2012' AND Date<'2/28/2013'
AND CAST(Date AS TIME) BETWEEN '20:00' AND '08:00'
Without seeing your specific error/unexpected results, I think the problem is that 20 is greater than 8.
You'll have to use two conditions:
where Date>= '2/01/2012' AND Date<'2/28/2013' AND (CAST(Date AS TIME) > '20:00' OR CAST(Date AS TIME) < '08:00')
EDIT: fixed condition
Is this what you are after?
WHERE Date BETWEEN '2012-01-01 20:00:00.000' AND '2012-12-01 08:00:00.000'
It is a little bit unclear whether you are attempting to generate the WHERE clause variables dynamically?
You need to combine your "date" and "time" parts together.
This code will illustrate how to do this:
SELECT the_date
, the_time
, DateAdd(hh, DatePart(hh, the_time), the_date) As hour_added
, DateAdd(mi, DatePart(mi, the_time), the_date) As minute_added
, DateAdd(mi, DatePart(mi, the_time), DateAdd(hh, DatePart(hh, the_time), the_date)) As both_added
FROM (
SELECT Cast('2013-02-28' As datetime) As the_date
, Cast('08:30' As datetime) As the_time
) As example
You can then use the resultant values in your comparison

Find the busiest period

I have a large table in MS SQL 2012 (40m records) containing call data. I would like to find the peak volume of calls, and the time that it occurred. If possible, I would also like to find the next 4 busiest periods.
I plan to use 3 columns:
CallID
DialTime
EndTime
The only way I can think to do this would be to do this:
Select '2013-07-01 00:00:01' as [Period], count([CallID]) as [Calls]
from [Table]
where DialTime <= '2013-07-01 00:00:01'
and EndTime >= '2013-07-01 00:00:01'
union
Select '2013-07-01 00:00:02' as [Period], count([CallID]) as [Calls]
from [Table]
where DialTime <= '2013-07-01 00:00:02'
and EndTime >= '2013-07-01 00:00:02'
union
etc
Can anyone suggest a better/more efficient way of doing this?
Try something like this. #time_begin and #time_end are the parameters that you can use for the interval of time for which you want to get the results.
with time_items (time_item) as
(
select #time_begin as time_item
union all
select dateadd(second,1,t.time_item) as time_item from time_items t where t.time_item<#time_end
)
select
time_items.time_item as [Period],
sum(case when [Table].DialTime<=time_items.time_item and [Table].EndTime>=time_items.time_item then 1 else 0 end) as [Calls]
from time_items
left outer join [Table] on 1=1
group by
time_items.time_item
order by
[Calls] desc;
You can use VALUES as Table Source
SELECT DialTime, EndTime, o.Calls
FROM (VALUES ('20130701 00:00:01', '20130701 00:00:01'),
('20130701 00:00:02', '20130701 00:00:02'))x(DialTime, EndTime)
CROSS APPLY(
SELECT COUNT(CallID) AS Calls
FROM [Table] t
WHERE DialTime <= x.DialTime
AND EndTime >= x.EndTime
) o