Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clean and Reshape Timesheet Data

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Clean and Reshape Timesheet Data. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Tessella's analytics team receives timesheet extracts with inconsistent formatting, duplicate updates, missing references, and mixed approval states. Write a PostgreSQL query that converts the raw extract into a monthly analysis-ready table.

Requirements

  1. Keep only the latest record for each timesheet_id, based on updated_at.
  2. Parse the date and decimal hours, normalize billable flags, and retain approved rows with hours from 0 through 24.
  3. Use left joins to retain approved entries even when the consultant or project reference is missing. Label missing references as Unknown consultant or Unknown project.
  4. Aggregate by month, consultant, and project, returning total, billable, non-billable hours, and entry count. Sort chronologically, then by consultant and project.

Schema

raw_timesheets
ColumnTypeDescription
timesheet_idINTLogical identifier for a timesheet entry
ingestion_idPKINTUnique raw ingestion row identifier
consultant_idINTConsultant reference
project_codeVARCHAR(20)Project reference
work_date_textVARCHAR(20)Work date stored as text in DD/MM/YYYY format
hours_textVARCHAR(20)Hours stored as text
billable_textVARCHAR(10)Text representation of billable status
statusVARCHAR(20)Timesheet approval status
updated_atTIMESTAMPTimestamp of the raw row update
consultants
ColumnTypeDescription
consultant_idPKINTConsultant identifier
consultant_nameVARCHAR(100)Consultant full name
projects
ColumnTypeDescription
project_codePKVARCHAR(20)Project code
project_nameVARCHAR(100)Project name
Tablesraw_timesheetsconsultantsprojects
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results