(Please note that I have seen a similar question on StackOverflow recently, however I can not find it anywhere - if anyone can find it please post the link)
I have a table that has a datetime field in it. For example:
Table data
DateTime date;
int number;
I need to find the difference between date in a row and date in the next row. I have tried with a query:
select date as current_date, date - (select top 1 date where date > current_date from data) as time_difference from date
however this won't work, because of "current_date". Is there a way I can do this with one query?
SELECT date AS current_date, next_date - date AS difference
FROM mytable mo
CROSS APPLY
(
SELECT TOP 1 date AS next_date
FROM mytable mi
WHERE mi.date > mo.date
ORDER BY
date
) q
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 of accident date I want to calculate the maximum between the difference of date i and date i + 1 which are in the same column. when we declare an accident date, I want to find the record of days without accidents.
You can use lag(). Assuming a table structure like mytable(dt), where dt is of a date-like datatype, you would do:
select max(diff)
from (select dt - lag(dt) over(order by dt) diff from mytable) t
I have a query where I have a list of ~ 20k users for a specific week of the month that represents that they have logged on to our site.
What I need to get - for each of these users, in the past 30 days if they have
1. logged on: defined by any rows recorded in the same table
2. max event in the 30 day window, prior to the date in the current where clause
This is the current code snippet that helps me narrow to the ~20k users for a given week to begin with:
select
user_id,
max(timestamp)
from table
where timestamp between '2019-02-01' and '2019-02-05'
group by 1,2;
Expected result set/columns:
user_id,
max(timestamp),
logged_on, [if they have any # of rows in the same table within 30 days prior to their max(timestamp) date]
previous_timestamp, [the 2nd most recent login date within 30 days prior to their max(timestamp) date]
I think this is what you're looking for. Not sure if it's the most efficient method though - perhaps windowing functions may perform better but like bob-mccormick mentioned: the tricky bit would be filling in dates where the user (partition key) was not active so that the range query will work correctly.
Example data setup (Snowflake syntax)
-- Create sample table
create temporary table user_logins (userid number, date_logged_on timestamp);
;
-- Insert some random sample data
insert overwrite into user_logins
select
uniform(1,10,random()) userid,
dateadd('minutes', uniform(1,86400,random()) * -1,current_timestamp::timestamp_ntz) date_logged_on
from table(generator(rowcount => 100))
;
Select statement
-- Run select
with user_last_logins as (
select
userid,
max(date_logged_on) last_login
from user_logins
where
date_logged_on between '2019-01-01' and '2019-05-08'
group by userid
)
select
user_last_logins.userid,
max(user_last_logins.last_login) last_logged_on,
count(prior_30_each_user.userid) num_logins_prior_30,
max(prior_30_each_user.date_logged_on)
from user_last_logins
left join user_logins prior_30_each_user
on user_last_logins.userid = prior_30_each_user.userid
and prior_30_each_user.date_logged_on > dateadd('day', -30, user_last_logins.last_login) and prior_30_each_user.date_logged_on < user_last_logins.last_login
group by user_last_logins.userid
;
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 exchange rate table in which there are multiple date wise records with exchange rate.
Date Rate
17/05/2012 5
23/05/2012 6
27/05/2012 7
Now I want rate while passing any date like if, I pass 20/05/2012 then rate 5 should return because 20/05/2012 elapse in date range 17 and 23 may 2012.
Assuming you have correct datatypes (that is, not varchar to store date values...)
SELECT TOP 1
Rate
FROM
MyTable
WHERE
DateColumn <= '20120520'
ORDER BY
DateColumn DESC
Something like this will work:
select Rate from tablename where Date in (
select max(Date) as Date
from tablename
where Date <= convert(datetime, '20/05/2012', 103)
)