postgresql
Postgres Tip and Tricks
Ricerca…
Alternativa al DATEADD in Postgres
-
SELECT CURRENT_DATE + '1 day'::INTERVAL
-
SELECT '1999-12-11'::TIMESTAMP + '19 days'::INTERVAL
-
SELECT '1 month'::INTERVAL + '1 month 3 days'::INTERVAL
Valori separati da virgola di una colonna
SELECT
string_agg(<TABLE_NAME>.<COLUMN_NAME>, ',')
FROM
<SCHEMA_NAME>.<TABLE_NAME> T
Elimina i record duplicati dalla tabella Postgres
DELETE
FROM <SCHEMA_NAME>.<Table_NAME>
WHERE
ctid NOT IN
(
SELECT
MAX(ctid)
FROM
<SCHEMA_NAME>.<TABLE_NAME>
GROUP BY
<SCHEMA_NAME>.<TABLE_NAME>.*
)
;
Aggiorna query con join tra due tabelle alternative poiché Postresql non supporta join nella query di aggiornamento.
update <SCHEMA_NAME>.<TABLE_NAME_1> AS A
SET <COLUMN_1> = True
FROM <SCHEMA_NAME>.<TABLE_NAME_2> AS B
WHERE
A.<COLUMN_2> = B.<COLUMN_2> AND
A.<COLUMN_3> = B.<COLUMN_3>
Differenza tra due date timestamp mese saggio e anno saggio
Differenza mensile tra due date (timestamp)
select
(
(DATE_PART('year', AgeonDate) - DATE_PART('year', tmpdate)) * 12
+
(DATE_PART('month', AgeonDate) - DATE_PART('month', tmpdate))
)
from dbo."Table1"
Differenza annuale tra due date (timestamp)
select (DATE_PART('year', AgeonDate) - DATE_PART('year', tmpdate)) from dbo."Table1"
Query su Copia / Sposta / Trasferisci i dati della tabella da un database ad un'altra tabella di database con lo stesso schema
Primo Esegui
CREATE EXTENSION DBLINK;
Poi
INSERT INTO
<SCHEMA_NAME>.<TABLE_NAME_1>
SELECT *
FROM
DBLINK(
'HOST=<IP-ADDRESS> USER=<USERNAME> PASSWORD=<PASSWORD> DBNAME=<DATABASE>',
'SELECT * FROM <SCHEMA_NAME>.<TABLE_NAME_2>')
AS <TABLE_NAME>
(
<COLUMN_1> <DATATYPE_1>,
<COLUMN_1> <DATATYPE_2>,
<COLUMN_1> <DATATYPE_3>
);
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow