Skip to content

Commit

Permalink
Quert and conditional SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
teguhteja committed Nov 16, 2024
1 parent 3369a2b commit 7d3c9c3
Showing 1 changed file with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,93 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating and Querying Match Statistics in SQL"
"## Creating and Querying Match Statistics in SQL\n",
"\n",
"Let's do some more debugging on the same query we saw before. As a reminder, the SQL query is designed to categorize match events based on the minute they occurred into 'Early', 'Mid', and 'Late' categories. However, there seems to be a bug in the query that is causing it to malfunction. Your task is to spot the issues and make the necessary corrections. Keep in mind there could be more than one issue.\n",
"\n",
"-- TODO: Categorize match events based on the minute they occurred\n",
"SELECT\n",
" m.match_id AS MatchID,\n",
" me.minute AS Minute,\n",
" CASE\n",
" WHEN me.minute < 30 'Early'\n",
" WHEN me.minute BETWEEN 30 AND 60 'Mid'\n",
" ELSE 'Late'\n",
" END EventTimeCategory\n",
"FROM\n",
" MatchEvents me\n",
"JOIN\n",
" Matches m ON me.match_id = m.match_id;\n",
"\n",
"To categorize match events based on the minute they occurred using the `CASE` statement, we need to correctly fill in the conditions and complete the `JOIN`. Here’s how you can complete the SQL query:\n",
"\n",
"```sql\n",
"SELECT\n",
" m.match_id AS MatchID,\n",
" me.minute AS Minute,\n",
" CASE\n",
" WHEN me.minute < 30 THEN 'Early'\n",
" WHEN me.minute BETWEEN 30 AND 60 THEN 'Mid'\n",
" ELSE 'Late'\n",
" END AS EventTimeCategory\n",
"FROM\n",
" MatchEvents me\n",
"JOIN\n",
" Matches m ON me.match_id = m.match_id;\n",
"```\n",
"\n",
"### Explanation:\n",
"1. `me.minute < 30`: This categorizes events occurring in the first 30 minutes as 'Early'.\n",
"2. `me.minute BETWEEN 30 AND 60`: This categorizes events occurring between the 30th and 60th minutes as 'Mid'.\n",
"3. `ELSE 'Late'`: Events that occurred after the 60th minute are labeled as 'Late'.\n",
"4. **FROM Clause**: `MatchEvents me` is the table that contains event details.\n",
"5. **JOIN Clause**: We are joining the `MatchEvents` table with the `Matches` table using the `match_id` field to relate events to their corresponding matches. \n",
"\n",
"This query will categorize each event into one of the three time categories based on the minute they occurred."
]
},
{
"cell_type": "markdown",
"id": "90d4a031",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"Now it's time to put everything you've learned into practice. Using the CASE statement, write a SQL query that categorizes match events based on the minute they occurred. Your task is to select match IDs (MatchID), event minutes (Minute), and categorize each event into one of three categories: 'Early', 'Middle', or 'Late'. The categorization should be based on the following conditions:\n",
"\n",
"If the event occurred before 30 minutes (me.minute < 30), label it as 'Early'.\n",
"If the event occurred between 30 and 60 minutes (me.minute BETWEEN 30 AND 60), label it as 'Mid'.\n",
"For events occurring after 60 minutes or exactly at 60 minutes (ELSE), label them as 'Late'.\n",
"\n",
"-- TODO: Categorize match events based on the minute they occurred\n",
"\n",
"Here's how you can write the SQL query to categorize match events using the `CASE` statement:\n",
"\n",
"```sql\n",
"SELECT\n",
" me.match_id AS MatchID,\n",
" me.minute AS Minute,\n",
" CASE\n",
" WHEN me.minute < 30 THEN 'Early'\n",
" WHEN me.minute BETWEEN 30 AND 60 THEN 'Mid'\n",
" ELSE 'Late'\n",
" END AS EventTimeCategory\n",
"FROM\n",
" MatchEvents me;\n",
"```\n",
"\n",
"### Explanation:\n",
"- **me.match_id AS MatchID**: Selects the match ID from the `MatchEvents` table and labels it as `MatchID`.\n",
"- **me.minute AS Minute**: Selects the minute of the event and labels it as `Minute`.\n",
"- **CASE Statement**:\n",
" - `WHEN me.minute < 30 THEN 'Early'`: Categorizes events occurring before the 30th minute as 'Early'.\n",
" - `WHEN me.minute BETWEEN 30 AND 60 THEN 'Mid'`: Categorizes events occurring between the 30th and 60th minute as 'Mid'.\n",
" - `ELSE 'Late'`: Categorizes events occurring after the 60th minute as 'Late'.\n",
"- **FROM MatchEvents me**: Specifies the `MatchEvents` table as the source of data.\n",
"\n",
"This query will categorize each match event into 'Early', 'Mid', or 'Late' based on the minute it occurred."
]
}
],
Expand Down

0 comments on commit 7d3c9c3

Please sign in to comment.