{"id":15003302,"url":"https://github.com/janluksoft/tsqlpivot","last_synced_at":"2026-03-13T09:33:47.017Z","repository":{"id":216181008,"uuid":"576212386","full_name":"janluksoft/TSqlPivot","owner":"janluksoft","description":"An example of a complex T-SQL query","archived":false,"fork":false,"pushed_at":"2022-12-14T21:53:45.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T14:22:16.481Z","etag":null,"topics":["dynamic-sql","ms-sql-server","sql","t-sql"],"latest_commit_sha":null,"homepage":"","language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/janluksoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-12-09T09:21:27.000Z","updated_at":"2022-12-14T20:39:37.000Z","dependencies_parsed_at":"2024-01-08T21:35:41.982Z","dependency_job_id":"58d7f3a0-57f1-406b-92f9-2b2c5052f4ae","html_url":"https://github.com/janluksoft/TSqlPivot","commit_stats":null,"previous_names":["janluksoft/tsqlpivot"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janluksoft%2FTSqlPivot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janluksoft%2FTSqlPivot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janluksoft%2FTSqlPivot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janluksoft%2FTSqlPivot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janluksoft","download_url":"https://codeload.github.com/janluksoft/TSqlPivot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243154763,"owners_count":20244987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamic-sql","ms-sql-server","sql","t-sql"],"created_at":"2024-09-24T18:57:52.903Z","updated_at":"2025-12-24T09:19:57.543Z","avatar_url":"https://github.com/janluksoft.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TSqlPivot\nAn example of a complex T-SQL query\n\n## About project\nThe project shows an example of a complex T-SQL query whose elements are not available in SQL itself. \nThese are variables, dynamic loops, queries, and subqueries. The original table [tb_speedcam_rec] \ncontains a list of video recordings from several speed cameras. The speed camera number [cam_id] \nregistration day [rec_start] and the size of the video file in bytes [fsize] are entered. \nThe query creates a pivot table summing up the size of the recording files for the speed camera number \n[consecutive rows] and the day of the month [columns]. \n\nThe query solves several difficulties one by one:\n- Unequal date with hours is converted to date only;\n- The volume of files is converted from int to floating decimal and expressed in the target number of gigabytes;\n- Rows with 'null' are eliminated;\n- A subquery is created which is then used by the second Select;\n- The parameter int @jmon is created, where the period is narrowed down to a month;\n- Grouping by cameras [jcam] and days [jdate_start] is created;\n- Dynamic subqueries are created for the query: two lists with a unique list of days and cameras;\n- From this, the Cartesian product of the columns [jSizeGb] and [jdate_start] is created (PIVOT pivot table);\n- Lists eliminate many Null values and format data to string.\n\n![](/Jpg/T-SQL_05.png)\n\n## Running the script\nTo check the operation of the script, open the [SQLQuery_SpeedCameras.sql] file in Microsoft SQL Server Management Studio and run 'Execute'.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanluksoft%2Ftsqlpivot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanluksoft%2Ftsqlpivot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanluksoft%2Ftsqlpivot/lists"}