I have a table like idsfortime:
epochtime id
1392951600 0
1392952500 15
1392953400 30
1392954300 45
1392955200 60
There is another table with the following columns :
15916B 5.1815954385269 1392977820
15965A 7.16797368783744 1392977880
16272B 10.6633890639568 1392977865
16707A 37.6028010736386 1392977785
16730A 9.42097617868767 1392977866
The last column in the above table denotes epoch time.
I am trying to find out those speeds (column 2 in above table) which lie between epochtime of table idsfortime and below table .
I am using the below query :
select t.speed from idsfortime t1 JOIN staging t where t1.epochtime >= t.time AND t1.epochtime <= t.time;
But, this doesnt work. Please suggest
Check out this post.
Merging two columns in hive and use between operator
I hope you will resolve your issue. Keep in mind that while trying to use BETWEEN operator on DATE, the format should match to DD-MM-YYYY and TIME should match to HH:MM:SS.
Related
Due to some activity in my project I want to run this query in some frequency and check "where my query can fetch table timestamp is <= 1 min from current timestamp"
SQL Query to check the updated data in the table.
Even though your question is incomplete, you haven't provided your existing table structure or any queries. I'm just giving you some generic solution here which should work as long as you can convert that based on your specific need.
so, you are trying to get the difference of two-time values in minutes
Time that record was saved
Current Time
If you have a table : LogRecords with below fields:
LogId
LogMessage
LogTimestamp
then you would write your query to pull last-minute logs as :
select * from LogRecords
where DATEDIFF(MINUTE, LogTimestamp , GETDATE()) <= 1
I haven't tested this code but it should be 99% similar if it won't work. Please try and let me know.
Objective: Pull the last 30 days of data from a table that has a variable end date
Background: I have a table that contains purchase information but this table is only updated every two weeks therefore there's a lag in the data. Some day it can be 14 days behind and others 13 or 15 days behind.
My table contains a DATE_KEY column which joins to the DATE_DIM table on this key which is where I pull my date field from. I would use GETDATE or CURRENT_DATE but this is not appropriate in my case due to the lag.
I am using Sybase IQ and I believe I can't use a select statements in the where clause to compare dates, I got the following error:
Feature, scalar value subquery (at line 63) outside of a top level SELECT list, is not supported.
This is what I was trying to do
WHERE
TIME.[DAY] >= DATEADD(dd,-30,( SELECT
MAX([TIME1].[DAY])
FROM DB.DATE_DIM TIME1
JOIN DB.PURCHASES PURC
ON TIME1.KEY = PURC.KEY))
How can I pull the most recent 30 days of data given the constraints above?
According to the Sybase IQ documentation, you can use a comparison to a subquery, hence you could add a join to the DATE_DIM to the main FROM clause, and then compare that to a subquery similar to yours, just with the DATEADD moved into it. In the following code, I assume the alias for DATE_DIM in the main FROM clause is TIME0.
WHERE
TIME0.[DAY] >= (SELECT DATEADD(dd,-30, MAX([TIME1].[DAY]))
FROM DB.DATE_DIM TIME1
JOIN DB.PURCHASES PURC
ON TIME1.KEY = PURC.KEY
)
I'm currently busy with some SQL (in both Netezza and HiveQL). Our company often uses numerical fields for dates (e.g. 20150602 for date, 20160400 for month).
I need to add 6 months to one of these dates, and usually I would do:
cast(to_char(add_months(to_date(A.date,'yyyyMMdd'),6),'yyyyMMdd') as int)
Although this isn't pretty, it works and I don't know any shorter or better way. My main problems is that this is a large table (12 billion rows) and date conversion isn't exactly snappy, and the query I'm working on is getting killed due to running to long. Is there some better way to do this?
I'd try a join to a time dimension table with these columns (ALL int):
MonthId StartDate EndDate
1 20150101 20150199
2 20150201 20150299
3 20150301 20150399
.
.
.
24 20161201 20161299
Then do this if you simply want the first day of the month as a result (will need a different timetable if you want to join-convert the full date, but this example is easyer to write on an iPad and can be to expanded):
select
t2.startdate,
y.*
from yourtable y
Join timetab t1
on y.dateAsInt between t1.StartDate and t1.EndDate
Join t2
On t1.monthid=t2.monthid-6
I have a table (lets call it AAA) containing 3 colums ID,DateFrom,DateTo
I want to write a query to return all the records that contain (even 1 day) within the period DateFrom-DateTo of a specific year (eg 2016).
I am using SQL Server 2005
Thank you
Another way is this:
SELECT <columns list>
FROM AAA
WHERE DateFrom <= '2016-12-31' AND DateTo >= '2016-01-01'
If you have an index on DateFrom and DateTo, this query allows Sql-Server to use that index, unlike the query in Max xaM's answer.
On a small table you will probably see no difference but on a large one there can be a big performance hit using that query, since Sql-Server can't use an index if the column in the where clause is inside a function
Try this:
SELECT * FROM AAA
WHERE DATEPART(YEAR,DateFrom)=2016 OR DATEPART(YEAR,DateTo)=2016
Well you can use the following query
select * from Table1
WHERE DateDiff(day,DateFrom,DateTo)>0
AND YEAR(DateFrom) = YEAR(DateTo)
And here is the result:
Enjoy :D !
This question already has answers here:
TSQL to combine a date field and a time field
(4 answers)
Closed 9 years ago.
I have a database that stores date and time in separate fields. I need to select all records that occurred within + and - 90 minutes of the date and time each of these happened.
I am able to get everything in the format I need to pull it off
SELECT UFV1.USR_DATE
,UFV1.USR_TIME
,LEFT(CAST(DATEADD(minute, -90, UFV1.USR_TIME) AS TIME),8) AS MIN_TIME
,LEFT(CAST(DATEADD(minute, +90, UFV1.USR_TIME) AS TIME),8)AS MAX_TIME
FROM USR_Y_FACILITY_VISIT UFV1
WHERE UFV1.MASTER_CUSTOMER_ID = '2'
ORDER BY UFV1.USR_DATE, UFV1.USR_TIME
Where I am stuck is I need to build a query that takes this info (basically the min/max from each line) then selects all the info in the same table based off that. Thank you for your help I am totally stumped as to where to go next.
Add the key fields for the table to your query as given, along with a corresponding GROUP BY clause. Then query your data table joind to that query as a sun-query on this format:
SELECT *
FROM T
JOIN ( SELECT * ... ) U
ON U.key = T.key
AND T.dateTime BETWEENU.MinDateTime AND U.MaxDateTime
If I correctly understood a question then, this request is necessary for you
SELECT *
FROM USR_Y_FACILITY_VISIT UFV1
WHERE EXISTS (
SELECT 1
FROM USR_Y_FACILITY_VISIT UFV2
WHERE UFV2.MASTER_CUSTOMER_ID = '2'
AND UFV1.USR_DATE = UFV2.USR_DATE
AND UFV1.USR_TIME BETWEEN CAST(DATEADD(minute, -90, UFV2.USR_TIME) AS time)
AND CAST(DATEADD(minute, 90, UFV2.USR_TIME) AS time)
)