{"id":24800503,"url":"https://github.com/codeofrahul/case-study-1-dannys-diner","last_synced_at":"2025-03-25T00:45:34.351Z","repository":{"id":204730466,"uuid":"712526635","full_name":"CodeofRahul/Case-Study-1-Dannys-Diner","owner":"CodeofRahul","description":"In this repository I have mentioned my SQL solutions for a Case Study (Danny's Diner). The Case Study Challenge got from 8weeksqlchallenge.com","archived":false,"fork":false,"pushed_at":"2023-11-01T07:25:23.000Z","size":213,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T03:19:00.646Z","etag":null,"topics":["8weeksqlchallenge","case-study","dataanalysis","pgadmin4","postgresql","sql"],"latest_commit_sha":null,"homepage":"https://8weeksqlchallenge.com/","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/CodeofRahul.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}},"created_at":"2023-10-31T16:35:09.000Z","updated_at":"2023-11-01T07:20:47.000Z","dependencies_parsed_at":"2023-11-01T08:27:12.419Z","dependency_job_id":null,"html_url":"https://github.com/CodeofRahul/Case-Study-1-Dannys-Diner","commit_stats":null,"previous_names":["codeofrahul/danny-s-diner","codeofrahul/case-study-1-dannys-diner"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeofRahul%2FCase-Study-1-Dannys-Diner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeofRahul%2FCase-Study-1-Dannys-Diner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeofRahul%2FCase-Study-1-Dannys-Diner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeofRahul%2FCase-Study-1-Dannys-Diner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CodeofRahul","download_url":"https://codeload.github.com/CodeofRahul/Case-Study-1-Dannys-Diner/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245377957,"owners_count":20605375,"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":["8weeksqlchallenge","case-study","dataanalysis","pgadmin4","postgresql","sql"],"created_at":"2025-01-30T03:19:00.852Z","updated_at":"2025-03-25T00:45:34.314Z","avatar_url":"https://github.com/CodeofRahul.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Case Study #1: Danny's Diner\n\n![Danny's Diner](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/4cfa9c25-d267-4794-b8d9-71b14adc6eba)\n\n## Introduction\n\nDanny seriously loves Japanese food so in the beginning of 2021, he decides to embark upon a risky venture and opens up a cute little restaurant that sells his 3 favourite foods: sushi, curry and ramen.\n\nDanny’s Diner is in need of your assistance to help the restaurant stay afloat - the restaurant has captured some very basic data from their few months of operation but have no idea how to use their data to help them run the business.\n\n## Problem statement\n\nDanny wants to use the data to answer a few simple questions about his customers, especially about their visiting patterns, how much money they've spent and also which menu items are their favourite. Having this deeper connection with his customers will help him deliver a better and more personalised experience for his loyal customers. He plans on using these insights to help him decide whether he should expand the existing customer loyalty program.\n\n## Datasets\n\nThree key datasets for this case study\n\n- sales: The sales table captures all customer_id level purchases with an corresponding order_date and product_id information for when and what menu items were ordered.\n- menu: The menu table maps the product_id to the actual product_name and price of each menu item.\n- members: The members table captures the join_date when a customer_id joined the beta version of the Danny's Diner loyalty program.\n\n## Case Study Questions\n\n1. What is the total amount each customer spent at the restaurant?\n2. How many days has each customer visited the restaurant?\n3. What was the first item from the menu purchased by each customer?\n4. What is the most purchased item on the menu and how many times was it purchased by all customers?\n5. Which item was the most popular for each customer?\n6. Which item was purchased first by the customer after they became a member?\n7. Which item was purchased just before the customer became a member?\n8. What is the total items and amount spent for each member before they became a member?\n9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?\n10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A and B have at the end of January?\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 1. What is the total amount each customer spent at the restaurant?\n\n```sql\n\nSELECT s.customer_id, SUM(m.price) as \"Total amount\"\nFROM sales AS s\nINNER JOIN menu AS m\nON s.product_id = m.product_id\nGROUP BY s.customer_id\nORDER BY s.customer_id ASC;\n\n```\n\n##### Result\n\n![question 1](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/148ea2b4-5e50-4f7d-9898-524104577b16)\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 2.\tHow many days has each customer visited the restaurant?\n\n```sql\n\nSELECT customer_id, count(DISTINCT order_date) as \"visit count\"\nFROM sales\nGROUP BY customer_id\nORDER BY customer_id ASC;\n\n```\n\n##### Result\n\n![question 2](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/528a1273-6aba-436d-bd09-46122ccccba6)\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 3.\tWhat was the first item from the menu purchased by each customer?\n\n```sql\n\nWITH first_rank as(\nSELECT s.customer_id, s.order_date, m.product_name,\ndense_rank() over(partition by s.customer_id order by s.order_date) as rank\nfrom sales as s\ninner join menu as m\non s.product_id = m.product_id)\n\nSELECT customer_id, product_name\nFROM first_rank\nWHERE rank = 1\nGROUP BY customer_id, product_name;\n\n```\n\n##### Result\n\n![question 3](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/357a1a58-98e0-47c0-97ea-47223454948d)\n\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 4.\tWhat is the most purchased item on the menu and how many times was it purchased by all customers?\n\n```sql\n\nSELECT m.product_name as \"most purchased item\", count(s.product_id) \"purchase count\"\nFROM sales as s\nINNER JOIN menu as m\nON s.product_id = m.product_id\nGROUP BY m.product_name\nORDER BY count(s.product_id) DESC\nLIMIT 1;\n\n```\n\n##### Result\n\n![question 4](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/b09c6738-1315-4867-bb68-c86040444731)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 5.\tWhich item was the most popular for each customer?\n\n```sql\n\nwith popular as (\n\tSELECT s.customer_id, m.product_name, count(s.product_id) as product_count,\n\tdense_rank() over(partition by s.customer_id order by count(s.product_id) desc) as rank\n\tFROM sales as s\n\tINNER JOIN menu as m\n\tON s.product_id = m.product_id\n\tgroup by s.customer_id, m.product_name)\n\t\nSELECT customer_id, product_name, product_count\nFROM popular\nWHERE rank = 1;\n\n```\n\n##### Result\n\n![question 5](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/a4d68fa9-5c6a-403c-a4aa-65b01dece995)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 6.\tWhich item was purchased first by the customer after they became a member?\n\n```sql\n\nwith first_purchase as (\n\tSELECT s.customer_id, s.product_id,\n\trow_number() over(partition by s.customer_id order by s.order_date) as row_num\n\tFROM sales as s\n\tINNER JOIN members as b\n\tON S.customer_id = b.customer_id and\n\ts.order_date \u003e b.join_date\n)\n\nSELECT customer_id, product_name\nFROM first_purchase as f\nINNER JOIN menu as m\nON f.product_id = m.product_id\nWHERE row_num = 1\nORDER BY customer_id asc\n\n```\n\n##### Result\n\n![question 6](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/864a6da0-bc2b-4403-8db4-3e3c9577a11e)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 7.\tWhich item was purchased just before the customer became a member?\n\n```sql\n\nWITH purchased as\n(SELECT s.customer_id, m.product_name,\ndense_rank() over(partition by s.customer_id order by s.order_date desc) as rn\nFROM sales as s\nINNER JOIN menu as m\nON s.product_id = m.product_id\nINNER JOIN members as mb\nON s.customer_id = mb.customer_id\nWHERE s.order_date \u003c mb.join_date)\n\nSELECT customer_id, product_name\nFROM purchased\nWHERE rn = 1;\n\n```\n\n##### Result\n\n![question 7](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/9b718237-dd72-46cd-8943-bd55ddb59d23)\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 8.\tWhat is the total items and amount spent for each member before they became a member?\n\n```sql\n\nSELECT s.customer_id,\ncount(m.product_name) as \"total items\",\nconcat('$ ', sum(m.price)) as \"amount spent\"\nFROM sales as s\nINNER JOIN menu as m\nON s.product_id = m.product_id\nINNER JOIN members as mb\nON s.customer_id = mb.customer_id\nWHERE s.order_date \u003c mb.join_date\nGROUP BY s.customer_id\nORDER BY s.customer_id asc;\n\n```\n\n##### Result\n\n![question 8](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/c17383ec-e767-4e7d-a46a-3ba2ac677bd2)\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 9.\tIf each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?\n\n```sql\n\nSELECT s.customer_id,\nsum(\nCASE\n\tWHEN m.product_name = 'sushi' THEN m.price * 10 * 2\n\tELSE m.price * 10\nEND\n) as points\nFROM sales as s\nINNER JOIN menu as m\nON s.product_id = m.product_id\nGROUP BY s.customer_id\nORDER BY s.customer_id asc;\n\n```\n\n##### Result\n\n![question 9](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/c2bbf7f4-b872-4010-9814-4c3352487661)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### 10.\tIn the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi - how many points do customer A       and B have at the end of January?\n\n```sql\n\nWITH last_day_cte as (\n\tSELECT join_date, (join_date + interval '6 days')::date as last_date,\n\tcustomer_id\n\tFROM members\n)\nSELECT ld.customer_id,\nsum(\n\tCASE\n\t\tWHEN order_date between join_date and last_date THEN price*10*2\n\t\tWHEN order_date not between join_date and last_date\n\t\tAND product_name = 'sushi' THEN price*10*2\n\t\tWHEN order_date not between join_date and last_date\n\t\tAND product_name not in ('sushi') THEN price*10\n\tEND\n) AS customer_points\nFROM sales as s\nINNER JOIN menu as m\nON s.product_id = m.product_id\nINNER JOIN last_day_cte as ld\nON ld.customer_id = s.customer_id\nAND order_date \u003c= '2021-01-31'\nAND order_date \u003e= join_date\nGROUP BY ld.customer_id\nORDER BY ld.customer_id;\n\n```\n\n##### Result\n\n![question 10](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/42c83342-5a44-4273-affe-99309ea56a12)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### Bonus Question 1\n\n##### Join All The Things\n\nCreate basic data tables that Danny and his team can use to quickly derive insights without needing to join the base tables using sql. Fill Member column as 'N' if the purchase was made before becoming a member and 'Y' if the purchase was made after joining membership.\n\n```sql\n\nSELECT s.customer_id, s.order_date, m.product_name, m.price,\n\tcase\n\t\twhen mem.join_date \u003e s.order_date then 'N'\n\t\twhen mem.join_date \u003c s.order_date then 'Y'\n\t\t else 'N'\n\t\t end as member\nFROM sales as s\nLEFT JOIN menu as m\nON s.product_id = m.product_id\nLEFT JOIN members as mem\nON s.customer_id = mem.customer_id\nORDER BY s.customer_id, s.order_date;\n\n```\n\n##### Result\n\n![Bonus 1](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/5489cc47-702e-4071-bed7-7a44d9329ab8)\n\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n#### Bonus Question 2\n\n##### Rank all the things\n\nDanny also requires further information about the ` ranking ` of customer products, but he purposely does not need the ranking for non-member purchases so he expects ` null ranking ` values for the records when customers are not yet part of the loyalty program.\n\n```sql\n\nWith customers_data as(\nSELECT s.customer_id, s.order_date, m.product_name, m.price,\n\tcase\n\t\twhen mem.join_date \u003e s.order_date then 'N'\n\t\twhen mem.join_date \u003c= s.order_date then 'Y'\n\t\t else 'N'\n\t\t end as member\nFROM sales as s\nLEFT JOIN menu as m\nON s.product_id = m.product_id\nLEFT JOIN members as mem\nON s.customer_id = mem.customer_id\nORDER BY s.customer_id, s.order_date)\n\nSELECT *, \n\tcase\n\twhen member = 'N' then NULL\n\telse rank() over(partition by customer_id, member order by order_date)\n\tend as ranking\nFROM customers_data;\n\n```\n\n##### Result\n\n![Bonus 2](https://github.com/CodeofRahul/Danny-s-Diner/assets/143285125/6f54b604-8d1f-453b-99d3-30e810fb13e8)\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeofrahul%2Fcase-study-1-dannys-diner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeofrahul%2Fcase-study-1-dannys-diner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeofrahul%2Fcase-study-1-dannys-diner/lists"}