Funktionen zur Datenbearbeitung in PostgreSQL
Brian Piccolo
Sr. Director, Digital Strategy
Die Funktionen EXTRACT(), DATE_PART() und DATE_TRUNC() entdecken
2005-05-13 08:53:53
2005 or 5 or 2 or Friday
2005-05-13 00:00:00
EXTRACT( Feld FROMQuelle )
SELECT EXTRACT(quarter FROM timestamp '2005-01-24 05:12:00') AS quarter;
DATE_PART('Feld', Quelle)
SELECT DATE_PART('quarter', timestamp '2005-01-24 05:12:00') AS quarter;
+---------+
| quarter |
|---------|
| 1 |
+---------+
Transaktionsdaten aus der Tabelle_payment_ des DVD-Verleihs
SELECT * FROM payment;
+--------------------------------------------------------------------------------+
| payment_id | customer_id | staff_id | rental_id | amount | payment_date |
|------------|-------------|----------|-----------|--------|---------------------|
| 1 | 1 | 1 | 76 | 2.99 | 2005-05-25 11:30:37 |
| 2 | 1 | 1 | 573 | 0.99 | 2005-05-28 10:35:23 |
| 3 | 1 | 1 | 1185 | 5.99 | 2005-06-15 0:54:12 |
+--------------------------------------------------------------------------------+
Daten aus der Tabelle payment nach Jahr und Quartal
SELECT
EXTRACT(quarter FROM payment_date) AS quarter,
EXTRACT(year FROM payment_date) AS year,
SUM(amount) AS total_payments
FROM
payment
GROUP BY 1, 2;
Ergebnisse
+---------------------------------+
| quarter | year | total_payments |
|---------|------|----------------|
| 2 | 2005 | 14456.31 |
| 3 | 2005 | 52446.02 |
| 1 | 2006 | 514.18 |
+---------------------------------+
Die Funktion DATE_TRUNC() schneidet Zeitstempel- oder Intervall-Datentypen ab.
SELECT DATE_TRUNC('year', TIMESTAMP '2005-05-21 15:30:30');Result: 2005-01-01 00:00:00
SELECT DATE_TRUNC('month', TIMESTAMP '2005-05-21 15:30:30');
----CODE_GLUE----
```out
Result: 2005-05-01 00:00:00
Funktionen zur Datenbearbeitung in PostgreSQL