Can anyone tell me how to write the SQL Query to calculate the time difference between 2 columns that are stored as DATETIME columns and get the result in minutes...
For example:
Table structure
ID, start-time, end-time
I want to do a select on a specific ID and perform a calculation of the end-time - start-time and return the result in minutes only.
MySql : TimeStampDiff:
SELECT TIMESTAMPDIFF(MINUTE, start_time, end_time)
FROM MyTable
WHERE ID = 1;
SqlServer: DATEDIFF
SELECT DATEDIFF(n, start_time, end_time)
FROM MyTable
WHERE ID = 1;
If you're using SQL Server, you can use DATEDIFF
SELECT DATEDIFF(minute, start-time, end-time) FROM tableName where id=1;
Related
Can anyone help me with SSIS Expression
I have this query in the expression:
Select period, * from table
I want to add a where clause to get period = yesterday
But, period column is in julian date format.
at the end i want the same result like this query
Select period, * from table
Where getdate() - 1
Thank you
you only can use you table date time column to compare with the getdate
Select period,
dataDate, *
from table
Where dataDate = getdate() - 1
I have a table as below. I want to query specific dates from the table. The dates are random and therefore i do not want to use the BETWEEN keyword. Also, the number of dates that i want to query could vary from one to many(for simplicity lets say 2 distinct dates.
create table temptable(id serial primary key not null,myTimestamp timestamp);
insert into temptable(myTimestamp) values ('2020-09-25 02:02:51.99');
insert into temptable(myTimestamp) values ('2020-08-24 12:20:51.111');
insert into temptable(myTimestamp) values ('2020-09-23 13:20:51.286');
The following query is executed hoping to get two distinct dates.
select *
from temptable
where myTimestamp::date = date '2020-09-23'
and myTimestamp::date = date '2020-08-24';
The above query executes on pgadmin but nothing is listed on the table. If i use OR operator, i can see one date returned but that i not what i want. I want both the dates
Please advice
Thanks
One method uses in:
select *
from temptable
where myTimestamp::date in (date '2020-09-23', date '2020-08-24');
However, this might be more efficient with inequalities for the date comparisons:
select *
from temptable
where (myTimestamp >= '2020-09-23'::timestamp and
myTimestamp < '2020-09-24'::timestamp
) or
(myTimestamp >= '2020-08-24'::timestamp and
myTimestamp < '2020-08-25'::timestamp
)
This is more index friendly.
Or if you prefer:
select *
from temptable tt join
(values ('2020-09-23'::timestamp), ('2020-08-24'::timestamp)
) v(ts)
on myTimestamp >= v.ts and
myTimestamp < v.ts + interval '1 day';
So, as it has been already commented, all you need is to replace and with or
select *
from temptable
where myTimestamp::date = date '2020-09-23'
or myTimestamp::date = date '2020-08-24';
Here is a demo:
DEMO
I have a table in SQL Server 2014 with time stamps.
This is my table:
I want to compare each time stamp from my table with a time stamp that I input and get from my table the time stamp for which the datediff(table_timeStamp, #myTimestamp) is the smallest. Hope it is clear what I want. This is for a function and I want to know how can I do that in the easiest way possible?
SELECT TOP 1 * ........... order by ABS(datediff(second,table_timeStamp, #myTimestamp) )
If you want the closest date previous to the date input:
;With cteTestDates As
(
Select *, DateDiff(Second,datefield1, '2016-07-01') DateDifference
From TestDates
)
Select Top 1 *
From cteTestDates
Where DateDifference >= 0
Order By DateDifference
If you want the closest date regardless if it is in the past or future:
;With cteTestDates As
(
Select *, ABS(DateDiff(Second,datefield1, '2016-07-03')) DateDifference
From TestDates
)
Select Top 1 *
From cteTestDates
Order By DateDifference
That query works much faster if table has an index on Time_Stamp column.
The winner query will ALWAYS do a table scan and NEWER use index at all.
SELECT TOP 1 Time_Stamp FROM
SELECT Max(Time_Stamp) as Time_Stamp FROM MyTable WHERE Time_Stamp < #myTimestamp
UNION
SELECT MIN(Time_Stamp) as Time_Stamp FROM MyTable WHERE Time_Stamp > #myTimestamp)
ORDER BY 1
I have one Change Report Table which has two columns ChangedTime,FileName
Please consider this table has over 1000 records
Here I need to query all the changes based on following factors
i) Interval (i.e-1mins )
ii) No of files
It means when we have given Interval 1 min and No Of files 10.
If the the no of changed files more than 10 in any of the 1 minute interval, we need to get all the changed files exists in that 1 minute interval
Example:
i) Consider we have 15 changes in the interval 11:52 to 11:53
ii)And consider we have 20 changes in the interval 12:58 to 12:59
Now my expected results would be 35 records.
Thanks in advance.
You need to aggregate by the interval and then do the count. Assuming that an interval starting at time 0 is ok, the following should work:
declare #interval int = 1;
declare #limit int = 10;
select sum(cnt)
from (select count(*) as cnt
from t
group by DATEDIFF(minute, 0, ChangedTime)/#interval
) t
where cnt >= #limit;
If you have another time in mind for when intervals should start, then substitute that for 0.
EDIT:
For your particular query:
select sum(ChangedTime)
from (select count(*) as ChangedTime
from [MyDB].[dbo].[Log_Table.in_PC]
group by DATEDIFF(minute, 0, ChangedTime)/#interval
) t
where ChangedTime >= #limit;
You can't have a three part alias name on a subquery. t will do.
Something like this should work:
You count the number of records using the COUNT() function.
Then you limit the selection with the WHERE clause:
SELECT COUNT(FileName)
FROM "YourTable"
WHERE ChangedTime >= "StartInteval"
AND ChangedTime <= "EndInterval";
Another method that is useful in a where clause is BETWEEN : http://msdn.microsoft.com/en-us/library/ms187922.aspx.
You didn't state which SQL DB you are using so I assume its MSSQL.
select count(*) from (select a.FileName,
b.ChangedTime startTime,
a.ChangedTime endTime,
DATEDIFF ( minute , a.ChangedTime , b.ChangedTime ) timeInterval
from yourtable a, yourtable b
where a.FileName = b.FileName
and a.ChangedTime > b.ChangedTime
and DATEDIFF ( minute , a.ChangedTime , b.ChangedTime ) = 1) temp
group by temp.FileName
I'm trying to write a SQL query which should just pick the count with specific date not time.
select count(*) from xyz where time='2010-01-21'
but it is not returning any results.
For SQL Server 2008, you should be able to use the date data type:
select count(*) from xyz where cast(time as date) = '2010-01-21'
If you have a date time field, and you wanted to match a date:
select count(*) from xyz where time BETWEEN '2010-01-21' AND '2010-01-22'
MYSQL Date Time ref
Try (MySQL)
SELECT COUNT(*) FROM xyz WHERE DATE(datetime_col) = '2010-01-21'
in T-SQL (MSSQL) (kinda ugly, but should work):
SELECT * FROM xyz WHERE CAST(CONVERT(varchar(8), datetime_col, 112) AS DATETIME) <= '2011-01-21'