I have the following data (all of the table DDL and data DML is available on the fiddle here (SQL Server) and here (PostgreSQL):
I already have solutions, this question is about efficiency and what the optimal way of doing this would be?
CREATE TABLE ticket
(
ticket_id INTEGER NOT NULL,
working_time VARCHAR (30) NULL DEFAULT NULL CHECK (working_time != '')
);
and data:
ticket_id working_time
18 20.02.2021,15:00,17:00
18 20.02.2021,15:00,17:00
18 20.02.2021,15:00,17:00
20 20.02.2021,12:00,14:15
20 _rubbish__ -- <--- deliberate
20 20.02.2021,12:00,14:15
20
20 21.02.2021,12:00,14:15
20 _rubbish__
20 21.02.2021,12:00,14:15
20
11 rows
The _rubbish__ entry in the data is deliberate - it's free text and I have to be able to cope with poor data!
Now, I want a result like this:
Ticket ID The date hrs_worked_per_ticket
18 2021-02-20 06:00:00
20 2021-02-20 04:30:00
20 2021-02-21 04:30:00
There's no need to tell me that the schema is appalling - I find the idea of storing a date (in non-ISO format) and the times like that in a single line to be abhorrent! There is no choice in this matter.
I have an answer of my own for both PostgreSQL and SQL Server (see below), but I would like to know if there's a more efficient way of doing this?