Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Clean and Reshape Timesheet Data
00:00
5 left

Clean and Reshape Timesheet Data

HardSQL · PostgreSQL

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.

Output details

  • Return columns in this order: month, consultant_name, project_code, project_name, total_hours, billable_hours, non_billable_hours, entry_count.
  • Break updated_at ties by descending ingestion_id.
  • Parse work_date_text as DD/MM/YYYY, replace decimal commas with periods, trim values, and treat blank hours as NULL.
  • Treat trimmed, case-insensitive yes, y, true, and 1 as billable; treat every other value as non-billable.
  • Match approved status case-insensitively after trimming, and exclude rows whose parsed hours are NULL.
  • Sort ascending by month, consultant_name, and project_code, using project_code to break consultant ties; preserve numeric precision without rounding.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.