Athena Partition - partition by any month and day - sql

I've read Partitioning data in Athena for query Amazon s3,
I want to create one table with yearly data and to be able to partitioned it by any month and day.
So in the end I would be able to query between desired dates from all the year as below:
Select ...
from ...
where date between between '2015-03-01' and '2015-06-31'
Where 'date' is the partition s3 folders.
I didn't find a way to partitioned date for month and day, meaning to partition the folders under 'year' folder.
Only succussed to partitioned specific dates as doc suggest:
ALTER TABLE orders ADD
PARTITION (dt = '2014-04-21') LOCATION 's3://.../2014/04/21/'
PARTITION (dt = '2016-05-15') LOCATION 's3://.../2016/05/15/'
I have data in the structure:
s3://.../2014/04/21/file.csv
I'm using the partition with Alter Table method as doc suggest.
Could you tell me if there's a good way to do what I need?

Related

How to partition Delta lake table by Year where I have date in my Delta table

I have a Delta lake table with columns like this.
I want to partition the table by year. For that do I need to add year column to my table or can I directly partition by year without creating year column. If so, how do I do that?

Changing the partition spec of a hive table and move data

I have a external hive table employee which is partitioned by extract_timestamp (yyyy-mm-dd hh:mm:ss) as below.
empid empname extract_time
1 abc 2019-05-17 00:00:00
2 def 2019-05-18 14:21:00
I am trying to remove the partition by extract_time and change it to year,month and day partition. I am following the below method for this.
1. Create a new table employee_new with partitions year month and day
create external table employee_new
(empid int,
empname string
)
partitioned by (year int,month int,day int)
location '/user/emp/data/employee_new.txt';
2. insert overwrite into employee_new by selecting data from employee table
insert overwrite into employee_new as select*,year(extract_time),month(extract_time)
,day(extract_time)
from employee
3. Drop employee and employee_new and create employee table on top of /user/emp/data/employee_new.txt
Please let me know if this method is efficient and if there are any better ways to do the same.
Partition by date yyyy-MM-dd only, if possible, if upstream process can write hour files to daily folders. For such a small table partitioning by year, month and day separately seems overkill. It will be still too many folders.
If table is partitioned by date yyyy-MM-dd, partition pruning will work for your usage scenario because you are querying by day or year or month.
To filter by year in this case you will provide
where date >= '2019-01-01' and date < '2020-01-01' condition,
to filter by month:
where date >= '2019-01-01' and date < '2020-02-01'
and day: where date = '2019-01-01'
Filesystem listing will work much faster.
And if it is not possible to redesign upstream process to write to yyyy-MM-dd folders then your new design as you described in the question (yyyy/MM/dd folders) is the only solution.

Split hive partition to create multiple partition

I have an external hive table which is partitioned on load_date (DD-MM-YYYY). however the very first period lets say 01-01-2000 has all the data from 1980 till 2000. How can I further create partitions on year for the previous data while keeping the existing data (data for load date greater than 01-01-2000) still available
First load the data of '01-01-2000' into a table and create a dynamic partition table partitioned by data '01-01-2000'. This might solve your problem.

How to query data from latest partition in interval partitioned table

I have an interval partitioned table: PARTITION_TEST.
I need to pick a data from the last partition. Is it possible, without refering to dba_tab_partition table?
This table will have only 5 partitions at a time. Is there any way to select data from 5th partition position ?
Something like,
SELECT * FROM PARTITION_TEST partition_position(5)?

Creating a table with date as range partition in SQL Server 2012

I am new to SQL Server coding. Please let me know how to create a table with range partition on date in SQL Server
A similar syntax in teradata would be the following (a table is created with order date as range partition over year 2012 with each day as single partition )
CREATE TABLE ORDER_DATA (
ORDER_NUM INTEGER NOT NULL
,CUST_NUM INTEGER
,ORDER_DATE DATE
,ORDER_TOT DECIMAL(10,2)
)
PRIMARY INDEX(ORDER_NUM)
PARTITION BY (RANGE_N ( ORDER_DATE BETWEEN DATE ‘2012-01-01’ AND DATE 2012-12-31 EACH INTERVAL ‘1’ DAY));
Thanks in advance
The process of creating partitioned table is described on MSDN as follows:
Creating a partitioned table or index typically happens in four parts:
1. Create a filegroup or filegroups and corresponding files that will hold the partitions specified by the partition scheme.
2. Create a partition function that maps the rows of a table or index into partitions based on the values of a specified column.
3. Create a partition scheme that maps the partitions of a partitioned table or index to the new filegroups.
4. Create or modify a table or index and specify the partition scheme as the storage location.
You can find code samples on MSDN.