Wprowadzenie do Redshift
Jason Myers
Principal Architect
SYSDATE – data i czas na początku transakcji-- Get current date and time
SELECT SYSDATE;
timestamp
============================
2024-01-27 20:05:55.976353
GETDATE() – data i czas na początku instrukcji, wymaga nawiasów-- Get current date and time
SELECT GETDATE();
timestamp
============================
2024-01-27 20:06:55.976353
Uwaga na funkcje działające tylko na węźle lidera!
DATEDIFF zamiast AGEGETDATE/SYSDATE zamiast funkcji specyficznych dla lidera:CURRENT_TIMECURRENT_TIMESTAMPISFINITELOCALTIMELOCALTIMESTAMPNOWTRUNC zwraca datę z znacznika czasu-- Get current date based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT TRUNC(SYSDATE);
2024-01-27
DATE_TRUNC('datepart', timestamp) – obcina do datepart (np. godzina, dzień)-- Truncate to hour based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_TRUNC('minute', SYSDATE);
2024-01-27 20:05:55
DATE_PART(datepart, date or timestamp) -- Get current month based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_PART(month, SYSDATE);
1
month, day, yeardayofweek, quarter, timezone-- Get current day of week based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATE_PART(dayofweek, SYSDATE);
6
DATE_CMP(date_1, date_2) – porównanie względne
Funkcje dla konkretnych typów
DATE_CMP_TIMESTAMPDATE_CMP_TIMESTAMPTZTIMESTAMP_CMPTIMESTAMP_CMP_TIMESTAMPTZTIMESTAMPTZ_CMP-- Compare 5 dates from a table based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT date_col,
TRUNC(SYSDATE) AS current_date,
DATE_CMP(date_col, SYSDATE)
FROM combined_history_projections
ORDER BY date_col
LIMIT 3;
date_col | current_date | date_cmp
===========|===============|==========
2024-01-26 | 2024-01-27 | -1
2024-01-27 | 2024-01-27 | 0
2024-01-28 | 2024-01-27 | 1
DATEDIFF(datepart, value_1, value_2)-- Days till end of first quarter based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT DATEDIFF(day,TRUNC(SYSDATE),'2024-03-31') AS days_diff;
days_diff
===========
64
DATEADD(datepart, quantity, value)-- Add week to a date based on
-- SYSDATE of 2024-01-27 20:05:55.976353
SELECT TRUNC(SYSDATE) AS todays_date,
TRUNC(DATEADD(week, 1, SYSDATE)) AS next_weeks_date;
todays_date | next_weeks_date
============|==================
2024-01-27 | 2024-02-03
-- Add year by months to a date
SELECT DATEADD(month, 12, '2024-02-29');
2025-02-28 00:00:00
-- Add year by year to a date
SELECT DATEADD(year, 1, '2024-02-29');
2025-03-01 00:00:00
Wprowadzenie do Redshift