Hql: How to goup time by 30 minutes - group-by

I need an Hql query that gives as an output time grouped by 30 minutes. I did a lot of reseaches I only found queries in Sql , I tried the following ones:
SELECT date_format(`date`, "YYYY-MM-dd hh:00:00.000 aa"),dateadd(minute,(datediff(minute,0,a.dt)/30)*30,0) FROM flux GROUP BY dateadd(minute,(datediff(minute,0,a.dt)/30)*30,0)
it gives me the following error:
Invalid function dateadd
I tried also this one:
SELECT date_format(`date`, "YYYY-MM-dd hh:00:00.000 aa") FROM flux GROUP BY second(`date`)%('30*60')
But it gives me the folowing error:
Error encountered near token ''30*60''
if you have any Idea on how I can solve my issue please HELP.
THANK YOU !

Convert it to seconds and divide it by 30*60 and use group by.
SQLfiddle
select from_unixtime(round(unix_timestamp(ts)/1800)*1800),count(*) as count_30,
from flux
group by round(unix_timestamp(ts)/1800)

Related

Report Today minus 1 day

need help with this query DB2 IBM
SELECT
ABALPH AS Kunde,
SDLITM AS Artikel,
SDDSC1 AS Beschreibung,
SDSOQS AS Menge, date(digits(decimal(SDIVD+1900000,7,0))) AS Invoice,
decimal(SDUPRC/10000,15,2) AS Einzelpreis,
decimal(SDAEXP/100,15,2) AS Gesamtpreis,
SDDOCO AS Dokument,
AIAC01 AS Region
Now my question is , how can I get the today date minus 1 day ?
Thank you so much
have test it with ADD_DAYS doesn´t work.
I do not see the point in your query where a date is referenced but your query seems incomlete anyways because join contions are missing.
In general you get todays date with current date and you get yesterday with current date - 1 day
Check out this query:
SELECT current date, current date - 1 day
FROM sysibm.sysdummy1
Per the DB2 documentation, ADD_DAYS is valid with negative numbers for previous days. See example 4 on that page. So usage like this should have worked:
ADD_DAYS(DATE(...), -1)
or
ADD_DAYS(CURRENT_DATE, -1)
You should post your specific attempted usage of it and the error that is reported when you try it.

How to solve 'ERROR: invalid value for "YYYY" in source string' when trying to get previous weeks data?

I'm trying to get the last week of data in a pipeline script but when I do this I get the above error.
The code I am using is:
where to_date(start_datetime, 'YYYYMMDDHH24MISS') >= date_trunc('week', current_date) - 7
and to_date(start_datetime, 'YYYYMMDDHH24MISS') < date_trunc('week', current_date)
I think the issue could be that the table doesn't ingest data for about 4 days after the fact (i.e if date is 12/12/22 then the most recent date is 08/12/22)
But I've tried it with getting the week that started 2 weeks ago but still get the same error.
Anyone know how to solve this?
Thanks
The problem is in the function to_date and your wrong input format.
If you have something like this in the start_datetime column 12/12/22, you need to specify another format
to_date('12/12/22', 'DD/MM/YY')
More about formats: https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html

Error while finding the time difference in SQL Sybase using DATEDIFF function

I have the following query to find the difference between two dates in minutes.
SELECT DATEDIFF(MINUTE,'28.11.2019 09:23:41:202',GETDATE()) AS time_difference
But, I am getting the error
Converting '28.11.2019 09:23:41:202 'to timestamp is not possible
SQLCODE = -157, ODBC 3 State = "07006"
The value '28.11.2019 09:23:41:202' is obtained by using GETDATE() function in a previous query.
What is wrong here? any help?
UPDATE:
The query works if the value '28.11.2019 09:23:41:202' is changed to "2019.11.28" format. As mentioned above,
The value "'28.11.2019 09:23:41:202'" is obtained from using the same function GETDATE() in a previous query.
You need to utilise a standard date-time format, for it to work. For example, see following query:
select DateDiff(minute, '2019-11-28 08:12:34', GetDate()) as time_difference
Example from Official documentation:
SELECT DATEDIFF(minute, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

How to fix inconsistent datatypes:expected NUMBER got DATE in Oracle

I am receiving an error on a query that I am running. My error is 'inconsistent datatypes:expected NUMBER got DATE'
I am able to successfully do it in SQL Server so I looked up how to update the query for Oracle syntax and I am running into issues.
I have tried TO_DATE of a date field minus TO_DATE of the SYSDATE and then tried to divide that by 365 for the number of days in a year.
SELECT min.Contact,
min.MIN_DATE,
COUNT(giv.ID) AS COUNT,
SYSDATE,
TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD') AS DATEDIFF,
COUNT(giv.ID)/(TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD')/365)
FROM giv JOIN min
ON giv.Contact=min.Contact
GROUP BY min.Contact,min.MIN_DATE;
It's the parenthesis. Division takes precedence over subtraction. Try this:
COUNT(giv.ID)/((TO_DATE(min.MIN_DATE,'YYYY-MM-DD')-TO_DATE(SYSDATE,'YYYY-MM-DD'))/365)

Add Filter to extract rows where the timestamp falls in between yesterday at 4 AM and today at 3 AM in Cognos

I am new to Cognos and I am trying to add a filter to a column that only allows rows that are in between Yesterday at 4 AM and today at 3 AM. I have a working query in db2 but when I try to add it to the filter in Cognos I get a parsing error. Also, I found in the properties that the data type for the column I am trying to filter to be Unknown (Unsupported) type. I started off by creating two Data Item Expressions for each time frame I am trying to limit the data by. But I got a parsing error on the first one:
[Presentation Layer].[Cr dtime]=timestamp(current date) - 1 day + 4 hour
This works in my db2 local test database but doesn't even compile in Cognos. I also tried casting the column into a timestamp but that isn't working either. Any help is appreciated. I also tried using the _add_days function but I still get a parsing error. Also sampling the column I get values that appear to be timestamps as this string: 2016-01-02T11:11:45.000000000
Eventually if I get the two filters working I expect the original filter to be close to this syntax:
[Presentation Layer].[Cr dtime] is between [Yesterday 4AM] AND [Today 3AM]
Here is your filter:
[Presentation Layer].[Cr dtime] between
cast(_add_hours(_add_days(current_date,-1),4),timestamp)
and
cast(_add_hours(current_date,3),timestamp)
This works because current_date in Cognos does not have a time component. If you were to cast it directly to a timestamp type you would see the time part of the date as 12:00:00.000 AM, or midnight. Knowing this we can then simply add how much time after midnight we want, cast as a timestamp type and use this in the filter.