Athena greater than condition in date column - sql

I have the following query that I am trying to run on Athena.
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > '2017-12-31'
GROUP BY observation_date
However it is producing this error:
SYNTAX_ERROR: line 3:24: '>' cannot be applied to date, varchar(10)
This seems odd to me. Is there an error in my query or is Athena not able to handle greater than operators on date columns?
Thanks!

You need to use a cast to format the date correctly before making this comparison. Try the following:
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > CAST('2017-12-31' AS DATE)
GROUP BY observation_date
Check it out in Fiddler: SQL Fidle
UPDATE 17/07/2019
In order to reflect comments
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > DATE('2017-12-31')
GROUP BY observation_date

You can also use the date function which is a convenient alias for CAST(x AS date):
SELECT *
FROM date_data
WHERE trading_date >= DATE('2018-07-06');

select * from my_schema.my_table_name where date_column = cast('2017-03-29' as DATE) limit 5

I just want to add my little words here, if you have date column with ISO-8601 format, for example: 2022-08-02T01:46:46.963120Z then you can use parse_datetime function.
In my case, the query looks like this:
SELECT * FROM internal_alb_logs
WHERE elb_status_code >= 500 AND parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') > parse_datetime('2022-08-01-23:00:00','yyyy-MM-dd-HH:mm:ss')
ORDER BY time DESC
See more other examples here: https://docs.aws.amazon.com/athena/latest/ug/application-load-balancer-logs.html#query-alb-logs-examples

Related

CAST a date in Presto to next count

I would like to query Athena with JSON files. I matched creation_date with id because I would like to get a heatmap where on Y axis I have month, on X axis there day and I count the id's inside. I created a table with 2 columns:
creation_date date, id int. Next I am query with the below code:
SELECT CAST(creation_date as DATE) as ad_creation,
COUNT(id) as Total_ads
FROM default.test
GROUP BY CAST(creation_at_first as DATE)
unfortunately I am getting this error:
DatabaseError: Execution failed on sql: SELECT CAST(creation_date as DATE) as ad_creation, COUNT(id) as Total_ads FROM default.testing_fresh_1 GROUP BY CAST(creation_date as DATE)
When I query Select * from...
I get results formatted like this:
creation_date
2018-07-01 02:02:09
2018-06-05 01:39:30
2018-05-16 21:28:48
2017-04-23 17:03:53
Any idea what I am doing wrong?
From your select * result set, I guess there isn't ID column in your table.
You can try to use COUNT(*) instead of COUNT(id)
SELECT CAST(creation_date as DATE) as ad_creation,
COUNT(*) as Total_ads
FROM default.test
GROUP BY CAST(creation_date as DATE)
Try below Code.
SELECT CAST(creation_date as DATE) as ad_creation,
COUNT(id) as Total_ads
FROM default.testing_fresh_1
GROUP BY ad_creation

DB2 SQL extract only month and year from date column

I trying to group by month and year from MGMOV00F table - date column is DTMOMM, trying the syntax below but still getting error: argument 1 of function year not valid, anybody help please ?
Are you sure your dealing with an actual date data type?
Given the WHERE DT01MM > '20160101'
It would appear to me you're dealing with character columns; since AFAIK DB2 won't implicitly cast '20160101' to date like it would '2016-01-01'.
That being the case, your task is easy. Simply substring the first 6 characters..
Selects count of month-year (m-yyyy) ordered by number of matches:
SELECT valid_from, COUNT(1) FROM (
SELECT (
EXTRACT(MONTH FROM valid_from)
CONCAT '-' CONCAT
EXTRACT(YEAR FROM valid_from)
) AS valid_from
FROM some_table NOLOCK
) as s
GROUP BY s.valid_from
ORDER BY 2 DESC;
Result:

Compute count and running total for date field in SQL

Here is my dilemma. I have a field in the SQL database, called booking_date. The date is in a format like this
2014-10-13 12:05:58.533
I would like to be able to compute a count of bookings for each date (not date time) as well as a running total.
So my report would look something like so
My SQL code is like so
SELECT
dbo.book.create_time,
replace(convert(nvarchar, dbo.book.create_time, 106),' ', '/') as bookingcreation,
count(*) as Book_Count
FROM
....tables here
However, my count calculation is counting the date based of this type of date format > 2014-10-13 12:05:58.533 which is not computing correctly.
So instead, I'm getting this:
Also, I am not sure how to compute the running total. But I first need to get the count correctly.
Any help is greatly appreciated.
You seem to be using SQL Server. To get the count by day:
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
ORDER BY create_date;
You can get the cumulative sum in SQL Server 2012+ using the cumulative sum function:
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count,
sum(count(*)) over (order by cast(dbo.book.create_time as date) ) as Running_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
ORDER BY create_date;
In earlier versions, you can do something similar with a correlated subquery or cross apply.
EDIT:
In SQL Server 2008, you can do:
WITH t as (
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
)
SELECT t.create_date, t.Book_Count,
(SELECT SUM(Book_Count)
FROM t t2
WHERE t2.create_date <= t.create_date
) as Running_Count
FROM t
ORDER BY create_date;
Try to use trunc(book.create_time) in your query instead of the conversion you're doing

How do I get a maximium daily value of a numerical field over a year in SQL

How do I get a maximium daily value of a numerical field over a year in MS-SQL
This would query the daily maximum of value over 2008:
select
datepart(dayofyear,datecolumn)
, max(value)
from yourtable
where '2008-01-01' <= datecolumn and datecolumn < '2009-01-01'
group by datepart(dayofyear,datecolumn)
Or the daily maximum over each year:
select
datepart(year,datecolumn),
, datepart(dayofyear,datecolumn)
, max(value)
from yourtable
group by datepart(year,datecolumn), datepart(dayofyear,datecolumn)
Or the day(s) with the highest value in a year:
select
Year = datepart(year,datecolumn),
, DayOfYear = datepart(dayofyear,datecolumn)
, MaxValue = max(MaxValue)
from yourtable
inner join (
select
Year = datepart(year,datecolumn),
, MaxValue = max(value)
from yourtable
group by datepart(year,datecolumn)
) sub on
sub.Year = yourtable.datepart(year,datecolumn)
and sub.MaxValue = yourtable.value
group by
datepart(year,datecolumn),
datepart(dayofyear,datecolumn)
You didn't mention which RDBMS or SQL dialect you're using. The following will work with T-SQL (MS SQL Server). It may require some modifications for other dialects since date functions tend to change a lot between them.
SELECT
DATEPART(dy, my_date),
MAX(my_number)
FROM
My_Table
WHERE
my_date >= '2008-01-01' AND
my_date < '2009-01-01'
GROUP BY
DATEPART(dy, my_date)
The DAY function could be any function or combination of functions which gives you the days in the format that you're looking to get.
Also, if there are days with no rows at all then they will not be returned. If you need those days as well with a NULL or the highest value from the previous day then the query would need to be altered a bit.
Something like
SELECT dateadd(dd,0, datediff(dd,0,datetime)) as day, MAX(value)
FROM table GROUP BY dateadd(dd,0, datediff(dd,0,datetime)) WHERE
datetime < '2009-01-01' AND datetime > '2007-12-31'
Assuming datetime is your date column, dateadd(dd,0, datediff(dd,0,datetime)) will extract only the date part, and then you can group by that value to get a maximum daily value. There might be a prettier way to get only the date part though.
You can also use the between construct to avoid the less than and greater than.
Group on the date, use the max delegate to get the highest value for each date, sort on the value, and get the first record.
Example:
select top 1 theDate, max(theValue)
from TheTable
group by theDate
order by max(theValue) desc
(The date field needs to only contain a date for this grouping to work, i.e. the time component has to be zero.)
If you need to limit the query for a specific year, use a starting and ending date in a where claues:
select top 1 theDate, max(theValue)
from TheTable
where theDate between '2008-01-01' and '2008-12-13'
group by theDate
order by max(theValue) desc

SQL ORDER BY date problem

Can you please help me in solving this problem. I am trying to order the results of an SQL query by date, but I'm not getting the results I need.
The query I'm using is:
SELECT date FROM tbemp ORDER BY date ASC
Results are:
01/02/2009
03/01/2009
04/06/2009
05/03/2009
06/12/2008
07/02/2009
Results should be:
06/12/2008
03/01/2009
01/02/2009
07/02/2009
I need to select the date in the format above.
Your help is much appreciated.
It seems that your date column is not of type datetime but varchar. You have to convert it to datetime when sorting:
select date
from tbemp
order by convert(datetime, date, 103) ASC
style 103 = dd/MM/yyyy (msdn)
It sounds to me like your column isn't a date column but a text column (varchar/nvarchar etc). You should store it in the database as a date, not a string.
If you have to store it as a string for some reason, store it in a sortable format e.g. yyyy/MM/dd.
As najmeddine shows, you could convert the column on every access, but I would try very hard not to do that. It will make the database do a lot more work - it won't be able to keep appropriate indexes etc. Whenever possible, store the data in a type appropriate to the data itself.
Unsure what dbms you're using however I'd do it this way in Microsoft SQL:
select [date]
from tbemp
order by cast([date] as datetime) asc
this works for me:
SELECT datefield
FROM myTable
ORDER BY CONVERT(DATE, datefield) ASC
Following answer may help you
perform your date ordering by your date identifier but use to_char() function in select clause and use some other identifier in select clause for date
e.g.
SELECT TO_CHAR(DISPDATE1,'DD/MM/YYYY') AS DISPDATE,
SUM(APPLCOUNT) AS APPLIED,
SUM(CONFCOUNT) AS CONFIRMED
FROM
(
SELECT COUNT(ID) AS APPLCOUNT,
0 AS CONFCOUNT,
STUDENT.APPLIED_ON AS DISPDATE1
FROM STUDENT
WHERE STUDENT.ID = P_ID
GROUP BY STUDENT.APPLIED_ON
UNION
SELECT 0 AS APPLCOUNT,
COUNT(ID) AS CONFCOUNT,
STUDENT.CONFIRMED_ON AS DISPDATE1
FROM STUDENT
WHERE STUDENT.ID = P_ID
GROUP BY STUDENT.CONFIRMED_ON
)
GROUP BY DISPDATE1
ORDER BY DISPDATE1;
SELECT CONVERT(char(19), CAST(date AS datetime), 101) as [date]
FROM tbemp
ORDER BY convert(datetime, date, 101) ASC
Try using this this work for me
select * from `table_name` ORDER BY STR_TO_DATE(start_date,"%d-%m-%Y") ASC
where start_date is the field name
I wanted to edit several events in descendant chonologic order, and I just made a :
select
TO_CHAR(startdate,'YYYYMMDD') dateorder,
TO_CHAR(startdate,'DD/MM/YYYY') startdate,
...
from ...
...
order by dateorder desc
and it works for me.
But surely not adapted for every case...
Just hope it'll help someone !
This may help you in mysql, php.
//your date in any format
$date = $this->input->post('txtCouponExpiry');
$day = (int)substr($date, 3, 2);
$month = (int)substr($date, 0, 2);
$year = (int)substr($date, 7, 4);
$unixTimestamp = mktime(0, 0, 0, $year, $day, $month);
// insert it into database
'date'->$unixTimestamp;
//query for selecting order by date ASC or DESC
select * from table order_by date asc;
try this
Order by Convert(datetime,#date) desc
this should work for your date format
order by convert(date, your_column, 104) desc
Casting/Converting can result in out of range exceptions that unfortunately are not always as simple as excluding nulls.
A simple alternative method, which avoids the cast, is:
SELECT date
FROM table
ORDER BY YEAR(date), MONTH(date), DAY(date) ASC;