{"id":15060635,"url":"https://github.com/thanhloc81/sql-project-bicycles-practise","last_synced_at":"2026-02-01T03:32:16.533Z","repository":{"id":247019580,"uuid":"824812158","full_name":"thanhloc81/SQL-Project-Bicycles-Practise","owner":"thanhloc81","description":"✨ Utilizing SQL to extract data following a simulated task involving the Sales and Product modules","archived":false,"fork":false,"pushed_at":"2024-07-06T06:39:48.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T07:12:16.539Z","etag":null,"topics":["adventureworks","bicycle","bigquery","google-cloud","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thanhloc81.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-06T02:48:29.000Z","updated_at":"2024-07-06T06:39:51.000Z","dependencies_parsed_at":"2024-10-12T21:01:54.766Z","dependency_job_id":"5bd7d82b-2bda-4eee-8ca4-e98e97e5c7c7","html_url":"https://github.com/thanhloc81/SQL-Project-Bicycles-Practise","commit_stats":null,"previous_names":["thanhloc81/sql-project-bicycles-practise"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thanhloc81/SQL-Project-Bicycles-Practise","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhloc81%2FSQL-Project-Bicycles-Practise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhloc81%2FSQL-Project-Bicycles-Practise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhloc81%2FSQL-Project-Bicycles-Practise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhloc81%2FSQL-Project-Bicycles-Practise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thanhloc81","download_url":"https://codeload.github.com/thanhloc81/SQL-Project-Bicycles-Practise/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanhloc81%2FSQL-Project-Bicycles-Practise/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260174059,"owners_count":22969870,"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":["adventureworks","bicycle","bigquery","google-cloud","sql"],"created_at":"2024-09-24T23:01:48.055Z","updated_at":"2026-02-01T03:32:16.489Z","avatar_url":"https://github.com/thanhloc81.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQL-Project-Bicycles-Practise\n✨ Using SQL to extract data following a simulated task\n\n[Link BigQuery](https://console.cloud.google.com/bigquery?sq=400862878778:f171ee5c3bd14daebaf2efc24e417309)\n## Dataset\n- Utilizing the Adventure Works dataset, which consists of two main modules: Sales and Product.\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/8511b949-1e13-4e47-8552-5f25303081e6)![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/8e19a434-ef19-4a63-b745-3764225890f3)\n\n## Tools\n- Google BigQuery\n\n## Practise\n### Question 1: Calc Quantity of items, Sales value \u0026 Order quantity by each Subcategory in L12M\n``````sql\nSELECT \n    EXTRACT(MONTH from SOD.ModifiedDate) Month,\n    EXTRACT(YEAR from SOD.ModifiedDate) Year,\n    FORMAT_TIMESTAMP('%b %Y', SOD.ModifiedDate) AS Period,\n    PPS.Name,\n    SUM(OrderQty) AS Qty_item,\n    SUM(LineTotal) AS Total_sales,\n    COUNT(DISTINCT SalesOrderID) AS Order_cnt\nFROM\n    adventureworks2019.Sales.SalesOrderDetail SOD\nLEFT JOIN\n    adventureworks2019.Production.Product PP \n        ON SOD.ProductID = PP.ProductID\nLEFT JOIN\n    adventureworks2019.Production.ProductSubcategory PPS\n        ON PPS.ProductSubcategoryID = CAST(PP.ProductSubcategoryID AS INT)\nWHERE CAST(SOD.ModifiedDate AS DATE) \u003e= (\n        SELECT \n            DATE_SUB(CAST(MAX(ModifiedDate) AS DATE), INTERVAL 12 MONTH)\n        FROM\n            adventureworks2019.Sales.SalesOrderHeader\n    )\nGROUP BY \n    1, 2, 3, 4\nORDER BY    \n    1, 2;\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/909680a8-6df7-4e88-98bf-bf0f8265e11b)\n\n### Question 2: Calc % YoY growth rate by SubCategory \u0026 release top 3 cat with highest grow rate. Round results to 2 decimal\n``````sql\nWITH cte AS (\n  SELECT \n    EXTRACT(YEAR FROM SOD.ModifiedDate) AS Year,\n    PPS.Name,\n    SUM(OrderQty) AS Qty_item\n  FROM\n    adventureworks2019.Sales.SalesOrderDetail SOD\n  LEFT JOIN \n    adventureworks2019.Production.Product PP \n        ON SOD.ProductID = PP.ProductID\n  LEFT JOIN\n    adventureworks2019.Production.ProductSubcategory PPS\n        ON PPS.ProductSubcategoryID = CAST(PP.ProductSubcategoryID AS INT)\n  GROUP BY \n    1, 2\n  ORDER BY \n    1 DESC\n)\nSELECT \n  Name,\n  Qty_item,\n  LEAD(qty_item, 1) OVER(PARTITION BY Name ORDER BY year DESC, Name ASC) AS Prv_qty,\n  ROUND((qty_item / LEAD(qty_item, 1) OVER(PARTITION BY Name ORDER BY year DESC, Name ASC)) - 1, 2) AS Qty_diff\nFROM \n  cte \nORDER BY \n  4 DESC\nLIMIT \n  3;\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/9334276f-378e-48d1-b464-4750dc8e70db)\n\n### Question 3: Ranking Top 3 TeritoryID with biggest Order quantity of every year. If there's TerritoryID with same quantity in a year, do not skip the rank number\n``````sql\nWITH cte AS (\n  SELECT \n    EXTRACT(YEAR FROM SOD.ModifiedDate) AS Year,\n    TerritoryID,\n    COUNT(OrderQty) AS Order_cnt\n  FROM \n        adventureworks2019.Sales.SalesOrderDetail SOD\n  LEFT JOIN \n        adventureworks2019.Sales.SalesOrderHeader SOH \n            ON SOD.SalesOrderID = SOH.SalesOrderID\n  GROUP BY \n    1, 2\n)\nSELECT *\nFROM (\n  SELECT *,\n         DENSE_RANK() OVER(PARTITION BY year ORDER BY order_cnt DESC) AS Rank\n  FROM \n    cte\n)\nWHERE Rank \u003c= 3\nORDER BY \n  1 DESC;\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/b6e01fd6-e9a2-43d9-bafd-f964a99e9bcf)\n\n### Question 4: Calc Total Discount Cost belongs to Seasonal Discount for each SubCategory\n``````sql\nSELECT \n    EXTRACT(YEAR FROM SOD.ModifiedDate) AS Year,\n    PPS.Name,\n    SUM(UnitPrice * DiscountPct * OrderQty) AS total_cost\nFROM \n    adventureworks2019.Sales.SalesOrderDetail SOD\nLEFT JOIN \n    adventureworks2019.Production.Product PP\n        ON SOD.ProductID = PP.ProductID\nLEFT JOIN \n    adventureworks2019.Production.ProductSubcategory PPS \n        ON PPS.ProductSubcategoryID = CAST(PP.ProductSubcategoryID AS INT)\nLEFT JOIN \n    adventureworks2019.Sales.SpecialOffer SSO \n        ON SSO.SpecialOfferID = SOD.SpecialOfferID\nWHERE \n    Type LIKE 'Seasonal Discount'\nGROUP BY \n    1, 2;\n\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/c4f88e23-77f8-40ce-be8f-991219fd8ee7)\n\n### Question 5: Retention rate of Customer in 2014 with status of Successfully Shipped (Cohort Analysis)\n``````sql\nWITH total_cus AS (\n  SELECT \n    EXTRACT(MONTH FROM ModifiedDate) AS month,\n    CustomerID\n  FROM \n    adventureworks2019.Sales.SalesOrderHeader\n  WHERE \n    EXTRACT(YEAR FROM ModifiedDate) = 2014\n    AND Status = 5\n)\n,first_buy AS (\n  SELECT \n    *,\n    ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY month) AS rk\n  FROM \n    total_cus\n)\nSELECT \n  t2.month month,\n  CONCAT('M-', t1.month - t2.month) SubsequentMonth,\n  COUNT(DISTINCT t2.CustomerID) NumberCustomer\nFROM \n  total_cus t1\nLEFT JOIN \n  first_buy t2 \n    ON t1.month \u003e= t2.month AND t2.CustomerID = t1.CustomerID\nGROUP BY \n  1, 2\nORDER BY \n  1, 2;\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/47b76fd9-3b97-45e6-a9ed-b920b3ce34e9)\n\n### Question 6: Trend of Stock level \u0026 MoM diff % by all product in 2011. If %gr rate is null then 0. Round to 1 decimal\n``````sql\nWITH cte AS (\n  SELECT \n    PP.Name,\n    EXTRACT(MONTH FROM PWO.ModifiedDate) AS Month,\n    EXTRACT(YEAR FROM PWO.ModifiedDate) AS Year,\n    SUM(StockedQty) AS Stock_current\n  FROM \n    adventureworks2019.Production.Product PP\n  LEFT JOIN \n    adventureworks2019.Production.WorkOrder PWO \n        ON PP.ProductID = PWO.ProductID\n  WHERE \n    EXTRACT(YEAR FROM PWO.ModifiedDate) = 2011\n  GROUP BY \n    1, 2, 3\n)\n\nSELECT \n  *,\n  CASE \n    WHEN (Stock_current - Stock_pre) * 100.0 / Stock_pre IS NOT NULL \n        THEN ROUND((Stock_current - Stock_pre) * 100.0 / Stock_pre, 1)\n    ELSE 0.0 \n  END AS Diff\nFROM (\n  SELECT \n    *,\n    LEAD(Stock_current, 1) OVER(PARTITION BY Name ORDER BY Month DESC) AS Stock_pre\n  FROM \n    cte\n)\nORDER BY \n  Name;\n\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/35ef7e9b-16ab-47eb-a730-448cc6e46ede)\n\n### Question 7: Calc Ratio of Stock / Sales in 2011 by product name, by month. Order results by month desc, ratio desc. Round Ratio to 1 decimal mom yoy\n``````sql\nWITH cte1 AS (\n    SELECT \n        EXTRACT(MONTH FROM SOD.ModifiedDate) AS Month,\n        EXTRACT(YEAR FROM SOD.ModifiedDate) AS Year,\n        PP.ProductID,\n        PP.Name,\n        SUM(SOD.OrderQty) AS Order_Qty\n    FROM \n        adventureworks2019.Sales.SalesOrderDetail SOD\n    LEFT JOIN \n        adventureworks2019.Production.Product PP \n            ON PP.ProductID = SOD.ProductID\n    WHERE \n        EXTRACT(YEAR FROM SOD.ModifiedDate) = 2011\n    GROUP BY \n        1, 2, 3, 4\n),\ncte2 AS (\n    SELECT \n        EXTRACT(MONTH FROM PWO.ModifiedDate) AS Month,\n        EXTRACT(YEAR FROM PWO.ModifiedDate) AS Year,\n        PWO.ProductID,\n        Name,\n        SUM(StockedQty) AS Stock_Qty\n    FROM \n        adventureworks2019.Production.WorkOrder PWO\n    LEFT JOIN \n        adventureworks2019.Production.Product PP \n            ON PP.ProductID = PWO.ProductID\n    WHERE \n        EXTRACT(YEAR FROM PWO.ModifiedDate) = 2011\n    GROUP BY \n        1, 2, 3, 4\n)\n\nSELECT \n    cte2.Month,\n    cte2.Year,\n    cte2.ProductID,\n    cte2.Name,\n    COALESCE(Order_Qty, 0) Total_Order_Qty,\n    COALESCE(Stock_Qty, 0) Total_Stock_Qty,\n    ROUND(Stock_Qty / NULLIF(Order_Qty, 0), 2) AS Ratio\nFROM \n    cte2\nLEFT JOIN \n    cte1 ON cte1.ProductID = cte2.ProductID \n        AND cte1.Month = cte2.Month\nORDER BY \n    1 DESC, 7 DESC;\n``````\n#### Results\n![image](https://github.com/thanhloc81/SQL-Project-Bicycles-Practise/assets/151768013/04c1a7d7-beee-4fc5-9d09-9f6f038e6c80)\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanhloc81%2Fsql-project-bicycles-practise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthanhloc81%2Fsql-project-bicycles-practise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanhloc81%2Fsql-project-bicycles-practise/lists"}