PostgreSQL Query Results : Visual Studio Code vs. pgAdmin - postgresql

I'm fairly new to the SQL world and have been learning by reading Practical SQL by Anthony DeBarros. In the book he uses pgAdmin for all the queries but I am used to having everything done on Visual Studio Code - I like the autocomplete function on VSC and understand the layout pretty well...
My question is: The query results in VSC differ in formatting from pgAdmin. For example, in VSC I get the following query result
but in pgAdmin, as is noted in the book, is the following:
The 'segment' column's values are easier to understand than in the VSC results.
The code to create the table and data is the following:
CREATE TABLE train_rides (
trip_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
segment text NOT NULL,
departure timestamptz NOT NULL,
arrival timestamptz NOT NULL
);
INSERT INTO train_rides (segment, departure, arrival)
VALUES
('Chicago to New York', '2020-11-13 21:30 CST', '2020-11-14 18:23 EST'),
('New York to New Orleans', '2020-11-15 14:15 EST', '2020-11-16 19:32 CST'),
('New Orleans to Los Angeles', '2020-11-17 13:45 CST', '2020-11-18 9:00 PST'),
('Los Angeles to San Francisco', '2020-11-19 10:10 PST', '2020-11-19 21:24 PST'),
('San Francisco to Denver', '2020-11-20 9:10 PST', '2020-11-21 18:38 MST'),
('Denver to Chicago', '2020-11-22 19:10 MST', '2020-11-23 14:50 CST');
SET TIME ZONE 'US/Central';
SELECT * FROM train_rides;
For the query, I used this code:
SELECT segment,
to_char(departure, 'YYYY-MM-DD HH12:MI a.m. TZ') AS departure,
arrival - departure AS segment_duration
FROM train_rides;
So the question is: why do they look different? Is it a formatting thing in VSC? I would much rather use VSC than pgAdmin for my queries as I stated before.

Related

How to change a timestamp with timezone to date (YYYY)

I'm trying to change an entire columns format into a smaller date format. I'm currently using PostgreSQL and need to change it from timestamp with timezone to an easier format, preferably one that just displays the year? How can I get this done?
Ive tried using to_char functions as well as date_parse to no avail. Im still very new to this so its stumping me at this point, any help is much appreciated
Wed Dec 31 2008 19:00:00 GMT -0500 (Eastern Time Zone) is the current format.
I just want to be able to turn that into the year, so 2008
Seeing that you're dealing with text, it's best to convert it to an actual timestamptz first, using to_timestamp(). Then you can alter the column, casting it in place: online demo
create table your_table(invoicedate text);
insert into your_table values
('Wed Dec 31 2008 19:00:00 GMT -0500 (Eastern Time Zone)');
alter table your_table
alter column invoicedate
type timestamptz using
(to_timestamp(invoicedate,'Dy Mon DD YYYY HH24:MI:SS AAA TZHTZM'));
alter table your_table
add column invoiceyear smallint;
update your_table set invoiceyear=extract(year from invoicedate);
Already mentioned extract() can get you the year out of a timestamptz.
Keep in mind that PostgreSQL will consume the timestamps and keep them internally as UTC. This means that if you want to process invoices differently based on their timezone of origin, you'll have to save that origin information in a separate column. Right now, extracting a year from that timestamp will give you 2009 instead of 2008, because that time in EST corresponds to 2009 in UTC. To get it as 2008, you'll have to read it as invoicedate at time zone 'EST'

Grafana PostgreSQL distinct on() with time series

I'm quite new to Grafana and Postgres and could use some help with this. I have a dataset in PostgreSQL with temperature forecasts. Mutiple forecasts are published at various points throughout the day (indicated by dump_date) for the same reference date. Say: at 06:00 today and at 12:00 today a forecast is published for tomorrow (where the time is indicated by start_time). Now I want to visualize the temperature forecast as a time series using Grafana. However, I only want to visualize the latest published forecast (12:00) and not both forecasts. I thought I would use DISTINCT ON() to select only the latest published forecast from this dataset, but somehow with Grafana this is not responding. My code in Grafana is as follows:
SELECT
$__time(distinct on(t_ID.start_time)),
concat('Forecast')::text as metric,
t_ID.value
FROM
forecast_table t_ID
WHERE
$__timeFilter(t_ID.start_time)
and t_ID.start_time >= (current_timestamp - interval '30 minute')
and t_ID.dump_date >= (current_timestamp - interval '30 minute')
ORDER BY
t_ID.start_time asc,
t_ID.dump_date desc
This is not working however since I get the message: 'syntax error at or near AS'. What should I do?
You are using Grafana macro $__time, so your query in the editor:
SELECT
$__time(distinct on(t_ID.start_time)),
generates SQL:
SELECT
distinct on(t_ID.start_time AS "time"),
which is incorrect SQL syntax.
I wouldn't use macro. I would write correct SQL directly, e.g.
SELECT
distinct_on(t_ID.start_time) AS "time",
Also use Generated SQL and Query inspector Grafana features for debugging and query development. Make sure that Grafana generates correct SQL for Postgres.

How do I make a simple day dimension table for data warehousing star schema with postgresql?

How would I go about creating and populating a simple DAY dimension table for a star schema in postgreSQL ?
It is for an intro course to data warehousing and so it only has a few fields but most of the examples online are very involved and seem very complicated for a beginner. This isn't for an assignment - it is for studying because I am trying to make my own simple Star Schema with a fact table so I can start getting comfortable with it.
Can anyone give me a simple example of how I'd create the table with just a few fields (day_key as the surrogate key, a string describing the day, and some integer values representing the days or months for example) so I can at least get started on understanding?
A very simple DAY dimension table that should work for most versions of PostgreSQL (I am using 10.5). This is just something that should help someone newer to Data Warehousing make a basic day dimension for use when just getting started.
Create a Day Table
CREATE TABLE day (
day_key SERIAL PRIMARY KEY, -- SERIAL is an integer that will auto-increment as new rows added
description VARCHAR(40), -- a 'string' for a description
full_date DATE, -- an actual date type
month_number INTEGER,
month_name VARCHAR(40),
year INTEGER
);
Inserting Rows into the Day dimension
INSERT INTO day(description, full_date, month_number, month_name, year)
SELECT
to_char(days.d, 'FMMonth DD, YYYY'),
days.d::DATE,
to_char(days.d, 'MM')::integer,
to_char(days.d, 'FMMonth'),
to_char(days.d, 'YYYY')::integer
from (
SELECT generate_series(
('2019-01-01')::date, -- 'start' date
('2019-12-31')::date, -- 'end' date
interval '1 day' -- one for each day between the start and day
)) as days(d);
Result
Notes:
Basically you are just using the rows generated by the nested SELECT generate_series(... to insert into the Day table.
I used the FM above twice to remove some of the white space padding automatically generated in some of these date formatting.
I'd recommend removing the INSERT INTO day(...) line the first time you do this just to make sure the format of each column is what you're after before inserting it into your table.
This is just what I've seen commonly used - check the PostgreSQL documentation has some more thorough and good examples of more ways to format date types and get all kinds of useful dimensions.

redshift convert_timezone does not work

When running Redshift queries using Razor SQL, UTC dates appear to be treated as being in the local timezone, complete with daylight saving times.
For example, running
SELECT 'first',CONVERT_TIMEZONE('UTC', 'America/New_York', '2016-03-27 06:00:00')
UNION
SELECT 'second', CONVERT_TIMEZONE('UTC', 'America/New_York', '2016-03-27 07:00:00')
returns the same time for each, 2016-03-27 03:00
New York actually changed to daylight saving time on the 13th March and this does work:
SELECT 'first',CONVERT_TIMEZONE('UTC', 'America/New_York', '2016-03-13 06:00:00')
UNION
SELECT 'second', CONVERT_TIMEZONE('UTC', 'America/New_York', '2016-03-13 07:00:00')
So this turned out to be a bug in the amazon redshift JDBCS driver. If you run the same query in the standard postgres JDBC drivers it works just fine.
I haven't had any feedback from Amazon on a possible fix.

Postgres split timestamp column to time column and date column

Hi as the title says I have a column named healthtime in my table which is of type timestamp without timezone i.e.
Healthime
2012-02-02 08:15:00
I would like to split this into two new columns in the same table, one date column and one time column i.e.
Date Time
2012-02-02 08:15:00
Please can someone advise me how to do this (preferably in a single query),
Thanks,
James
Not one query, but one transaction... should be fine as well:
My table looks like:
CREATE TABLE d (t timestamp);
Code for changing the table:
BEGIN;
ALTER TABLE d ADD COLUMN dat date;
ALTER TABLE d ADD COLUMN tim time;
UPDATE d
SET
dat = t::date,
tim = t::time;
ALTER TABLE d DROP COLUMN t;
COMMIT;
SELECT Healthime::date from tbl_yourtable as date
SELECT Healthime::time(0) from tbl_yourtable as time
PostgreSQL Version: >= 10
select fake_table.healthtime
, cast(to_char(fake_table.healthtime, 'YYYY-MM-DD') as date) as "Date"
, cast(to_char(fake_table.healthtime, 'HH24:MI:SS') as time) as "Time"
from (select current_timestamp as "healthtime") as "fake_table"
output:
healthtime Date Time
timestamp with time zone date time without time zone
========================== ========== ======================
2022-04-22 14:20:25.678-04 2022-04-22 14:20:25
tested on PostgreSQL 9.3.25, compiled by Visual C++ build 1600, 64-bit running on Windows 8.1