Use LIKE, BETWEEN, TIMESTAMP for Athena in a Presto SQL statement? - presto

How would I make the usage start date the current date - 2 days, and have the timestamp of the start date include between 00:00:000 and 23:59:59.999? I have to use LIKE and % since usually the timestamp includes the year, month, day but I don't want to have to insert that each time I run this.
WHERE line_item_usage_start_date = current_date - interval '2' day
AND line_item_usage_start_date BETWEEN(LIKE(TIMESTAMP '%00:00:00.000%' and TIMESTAMP '%2020-08-25 23:59:59.999%)';

If I understand correctly, line_item_usage_start_date is of type timestamp and you want to get all rows that fall somewhere between the beginning of the day and the end of the day two days ago. In that case, there are a couple of ways to go about this:
You can convert the timestamp to a date by casting and then match it against the date two days ago:
WHERE cast(line_item_usage_start_date AS DATE) = current_date - INTERVAL '2' DAY
Use current_timestamp and date_trunc to compute the timestamp corresponding to the beginning of the day and constrain your results to the range between the beginning of the day 2 days ago and the beginning of the day 1 day ago:
WHERE line_item_usage_start_date >= date_trunc('day', current_timestamp - INTERVAL '2' DAY) AND
line_item_usage_start_date < date_trunc('day', current_timestamp - INTERVAL '1' DAY)

Related

Count records for each day between the startdate and enddate in sequelize ORM (Performing this query in nodejs)

I am passing start_time and end_time as a query parameter in an API and want to have count of the records on each day from start_time and end_time.
It is giving me total count of records between the specified start and end_time but I want count for each day individually.
Example start_time: 2017-10-07 and end_time is 2017-10-10
So, I want count for the number of records on the date: 2017-10-07, 2017-10-08, 2017-10-09 and 2017-10-10.
You want a GROUP BY clause in your sql to group your count by days. Here's documentation on how to do that with Sequelize.
https://sequelize.org/master/manual/model-querying-basics.html#grouping
And if your date isn't just a single day, but has hours and minutes, you might need to do some date truncation in your group by. Here's some info on that:
Sequelize grouping by date, disregarding hours/minutes/seconds

HIVE where date filter by x days back? string format

so our DBA's setup our hive table with the date column as the partition column, but as a "string" YYYYMMDD format.
How can I WHERE filter this "date" column for something like last 30 days?
Please use date_format to format systemdate - 30 days into YYYYMMDD and then compare with your partition column. Please note to use partition column as is so hive can choose correct partitions.
When you want to pick previous 30th days data -
select *
from mytable
where partition_col = date_format( current_date() - interval '30' days, 'yyyyMMdd')
If you want all data since last 30 days -
select *
from mytable
wherecast(partition_col as INT) >= cast(date_format( current_date() - interval '30' days, 'yyyyMMdd') as INT)
casting shouldnt impact partition benefits but you need to check the performance before using it. Please get back in such scenario.

How to find date periods between 2 dates?

I have 2 dates one is stored inside my date and for other date I am using calculated column in order to store the end date into that, how an I calculate the difference in time period between those dates, I need the date period between all those dates is that possible with DAX?
How can I use calculated column inside my DAX and also I dont have a calender table inside my database.
2019-05-31 and end date is 2019-06-03 then the difference will give me 3 dates that is 2019-05-31,2019-06-01 2019-06-02 and 2019-06-03
Totally possible and easy. If you just need the difference between dates in two columns you can create a calculated column using the following:
DateDiff =
DATEDIFF ( 'Table'[Date1], 'Table'[Date2], DAY )
This will take the difference between Date1 and Date2 in days.
DECLARE #start_date [date] = CAST(‘2012-08-01’ as [date])
DECLARE #end_date [date] = CAST(‘2012-09-01’ as [date])
SELECT
DATEADD(day, [v].[number], #start_date)
FROM
[master].[dbo].[spt_values] [v]
WHERE
[v].[type] = ‘P’ AND
DATEADD(day, [v].[number], #start_date) <= #end_date

To_char function in databricks

I am using sql as language for my notebook in databricks.
Want to get the day of week from the date given.
For doing this i used to_char(date,'fmday'). Getting error as function is not registered as temporary or permanant in databricks. Is there a way to get the name of day by other means.
Date is in format yyyymmdd
You are getting that error because to_char is not a SparkSQL function. You can see the list of functions in the ScalaDocs here: https://spark.apache.org/docs/latest/api/scala/org/apache/spark/sql/functions$.html
If your date is a DateType, you can do dayofweek(date) in SparkSQL.
get the name of the day
Being as you want to get the name of the day, you can use the date_format function with the argument 'EEEE' to get the day name, eg Monday. If you want to pass in an integer (eg numbers between 1 and 7) then you could just code a CASE statement, something like:
%sql
SELECT
dayofweek( CAST( '2018-12-31' AS DATE ) ) AS d,
date_format( CAST( '2018-12-31' AS DATE ), 'EEEE' ) AS dayname,
CASE dayofweek( CAST( '2018-12-31' AS DATE ) )
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'Unknown'
END AS caseTest
NB I have coded the CASE to start the week from Day 1 - Monday, which is different to the dayofweek default; this might be one reason to do that, ie you want a different default.
My Results:
I got a way to get the name of day of week as below
date_format(to_date('20170821','yyyyMMdd'),'EEEE')
Now i want to pass a column of integer datatype, but when i pass it to query getting null as output. Could someone please help

PowerPivot - relating data by date range

Does anyone know what DAX function I should use to display information from one table in another table.
I've got 2 tables in my data model:
Tasks
- Task ID
- Task Name
- Start Date
- End Date
Fiscal Periods
- ID
- Period Name
- Start Date
- End Date
What I'm trying to do is for each Task, add a calculated column the is populated with the corresponding Fiscal Period ID. I'm trying to add a filter or calculation that specifies:
- if the task start date is between Fiscal Start Date and Fiscal End Date, return the fiscal period id.
Anyone have any ideas?
Thanks,
Ro
This might help:
In case Fiscal Period ID is a number:
=CALCULATE(MAX(Periods[ID]),FILTER(Periods,Periods[Start Date]<=Tasks[Start Date] && Periods[End Date]>=Tasks[Start Date]))
In case Fiscal Period ID is not a number:
Put Start Date to calculated column first, say 'Period Start Date'
=CALCULATE(MAX(Periods[Start Date]),FILTER(Periods,Periods[Start Date]<=Tasks[Start Date] && Periods[End Date]>=Tasks[Start Date]))
and then use LOOKUPVALUE for ID
=LOOKUPVALUE(Periods[ID],Periods[Start Date],Tasks[Period Start Date])

Resources