In redshift, I can do something like this
date_part(epoch, '2019-03-07 10:17:03.000000') and it would return 1551953823. How do I do this in Presto?
You can either use to_unixtime:
presto> select to_unixtime(timestamp '2019-03-07 10:17:03');
_col0
---------------
1.551953823E9
(1 row)
or use date_diff to subtract the timestamp from the epoch timestamp:
presto> select date_diff('second', timestamp '1970-01-01', timestamp '2019-03-07 10:17:03');
_col0
------------
1551953823
(1 row)
Note that to_unixtime returns a double representing the number of seconds plus milliseconds as a fractional part. If you want just the seconds, you can cast the result to BIGINT:
presto> select cast(to_unixtime(timestamp '2019-03-07 10:17:03') as bigint);
_col0
------------
1551953823
(1 row)
Wondering how to filter for records since beginning of current year?
My query is as follows:
SELECT date_id, item_id, product_name, product_price, sum(order*product_price) as revenue
FROM sales_table
WHERE year(date_id) >= date_trunc('year', current_date)
There is an error on the 'year' when I executed this code.
Could someone advise? Thanks a lot!
Your approach is correct except that date_trunc returns a date, not year number:
presto:tiny> SELECT date_trunc('year', current_date);
_col0
------------
2020-01-01
Assuming date_id is date data type, your query will be:
SELECT * FROM sales_table WHERE date_id >= date_trunc('year', current_date);
I have a table with a column of type timestamp and when I insert a value, Scylla saves it with 3 zeros more.
According to Scylla Documentation (https://docs.scylladb.com/getting-started/types/#timestamps):
Timestamps can be input in CQL either using their value as an integer, or using a string that represents an ISO 8601 date. For instance, all of the values below are valid timestamp values for Mar 2, 2011, at 04:05:00 AM, GMT:
1299038700000
'2011-02-03 04:05+0000'
'2011-02-03 04:05:00+0000'
'2011-02-03 04:05:00.000+0000'
'2011-02-03T04:05+0000'
'2011-02-03T04:05:00+0000'
'2011-02-03T04:05:00.000+0000'
So when I create a table for example:
CREATE TABLE callers (phone text, timestamp timestamp, callID text, PRIMARY KEY(phone, timestamp));
And insert values into it:
INSERT INTO callers (phone, timestamp, callID) VALUES ('6978311845', 1299038700000, '123');
INSERT INTO callers (phone, timestamp, callID) VALUES ('6978311846', '2011-02-03 04:05+0000', '456');
INSERT INTO callers (phone, timestamp, callID) VALUES ('6978311847', '2011-02-03 04:05:00.000+0000', '789');
The SELECT * FROM callers; will show all timestamps with 3 zeros more after the dot:
phone | timestamp | callid
------------+---------------------------------+--------
6978311847 | 2011-02-03 04:05:00.000000+0000 | 789
6978311845 | 2011-03-02 04:05:00.000000+0000 | 123
6978311846 | 2011-02-03 04:05:00.000000+0000 | 456
As a result when I try for example to delete a row:
DELETE FROM callers WHERE phone = '6978311845' AND timestamp = '2011-03-02 04:05:00.000000+0000';
An error occurs:
InvalidRequest: Error from server: code=2200 [Invalid query] message="marshaling error: unable to parse date '2011-03-02 04:05:00.000000+0000': marshaling error: Milliseconds length exceeds expected (6)"
How can I store timestamp without getting this error?
You have hr:min:sec.millisec -> Millisec can be up to 999/1000 so essentially that's what the error is saying.
The 3 INSERT statement you did are correct in terms of syntax.
The DELETE statement should be in the same format as the INSERT:
AND timestamp = '2011-03-02 04:05:00.00+0000'
AND timestamp = '2011-03-02 04:05:00.000+0000'
AND timestamp = '2011-03-02 04:05:00+0000'
AND timestamp = '2011-03-02 04:05:00'
The additional 000 that appear in the table are just a display issue.
My query is about to get row number as SN in a SQL query from access database in which I get total sales for the day group by [bill Date] clause
my Working Query is:
sql = "SELECT [Bill Date] as [Date], Sum(Purchase) + Sum(Returns) as [Total Sales] FROM TableName Group By [Bill Date];"
I found this Row_Number Clause over Internet and i tried like this.
sql = "SELECT ROW_NUMBER() OVER (ORDER BY [Bill Date]) AS [SN], [Bill Date] as [Date], Sum(Purchase) + Sum(Returns) as [Total Sales] FROM TableName Group By [Bill Date];"
when i Run the about Code i get this error.
-2147217900 Syntax error (missing operator) in query expression ROW_NUMBER() OVER (ORDER BY [Bill Date]);"
i am using Excel Vba to connect to Access Database
Could any one help me to to get it in correct order.
Looks like you aren't define DESC or ASC on (ORDER BY [Bill Date]), need be something like this (ORDER BY [Bill Date] DESC) or (ORDER BY [Bill Date] ASC)
I have table in Cassandra:
CREATE TABLE test (
bucket int,
start timestamp,
end timestamp,
PRIMARY KEY((bucket),start, end)
);
And I would like to get a query like that:
SELECT * FROM test where bucket = 1 and start <= current_time and end >= current_time
In another word, I would like to find interval which contains given timestamp.
I know that query is wrong. I tried to do it also with Multi-column slice restrictions, but it's also useless in this case. Is there some way to do this?
I have worked a similar problem in the past, too. The way I did it, was to use a column to track start/end. Then have one row for the start of an event and one row for the end. Let's try creating a table to store "Nerd Holidays," like this:
CREATE TABLE nerd_holidays (
month_bucket int,
event_time timestamp,
beginend text,
name text,
PRIMARY KEY ((month_bucket), event_time, beginend)
) WITH CLUSTERING ORDER BY (event_time DESC, beginend ASC);
And I'll insert some rows:
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (3,'2018-03-14 00:00:00','begin','Pi Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (3,'2018-03-14 23:59:59','end','Pi Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (5,'2018-05-04 00:00:00','begin','Star Wars Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (5,'2018-05-04 23:59:59','end','Star Wars Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-19 00:00:00','begin','Talk Like a Pirate Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-19 23:59:59','end','Talk Like a Pirate Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-25 00:00:00','begin','Hobbit Day');
INSERT INTO nerd_holidays (month_bucket, event_time, beginend, name)
VALUES (9,'2018-09-25 23:59:59','end','Hobbit Day');
Now I can query data for a specific time in the month of September, like this:
cassdba#cqlsh:stackoverflow> SELECT * FROM nerd_holidays
WHERE month_bucket=9
AND event_time >= '2018-09-18 00:00'
AND event_time <= '2018-09-19 08:33' ;
month_bucket | event_time | beginend | name
--------------+---------------------------------+----------+------------------------
9 | 2018-09-19 05:00:00.000000+0000 | begin | Talk Like a Pirate Day
(1 rows)
As you can see, "Talk Like a Pirate Day" begins within the requested date range.