{ "data": { "question": { "questionId": "1801", "questionFrontendId": "1661", "boundTopicId": null, "title": "Average Time of Process per Machine", "titleSlug": "average-time-of-process-per-machine", "content": "

Table: Activity

\n\n
\n+----------------+---------+\n| Column Name    | Type    |\n+----------------+---------+\n| machine_id     | int     |\n| process_id     | int     |\n| activity_type  | enum    |\n| timestamp      | float   |\n+----------------+---------+\nThe table shows the user activities for a factory website.\n(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.\nmachine_id is the ID of a machine.\nprocess_id is the ID of a process running on the machine with ID machine_id.\nactivity_type is an ENUM (category) of type ('start', 'end').\ntimestamp is a float representing the current time in seconds.\n'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.\nThe 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.
\n\n

 

\n\n

There is a factory website that has several machines each running the same number of processes. Write a solution to find the average time each machine takes to complete a process.

\n\n

The time to complete a process is the 'end' timestamp minus the 'start' timestamp. The average time is calculated by the total time to complete every process on the machine divided by the number of processes that were run.

\n\n

The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places.

\n\n

Return the result table in any order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nActivity table:\n+------------+------------+---------------+-----------+\n| machine_id | process_id | activity_type | timestamp |\n+------------+------------+---------------+-----------+\n| 0          | 0          | start         | 0.712     |\n| 0          | 0          | end           | 1.520     |\n| 0          | 1          | start         | 3.140     |\n| 0          | 1          | end           | 4.120     |\n| 1          | 0          | start         | 0.550     |\n| 1          | 0          | end           | 1.550     |\n| 1          | 1          | start         | 0.430     |\n| 1          | 1          | end           | 1.420     |\n| 2          | 0          | start         | 4.100     |\n| 2          | 0          | end           | 4.512     |\n| 2          | 1          | start         | 2.500     |\n| 2          | 1          | end           | 5.000     |\n+------------+------------+---------------+-----------+\nOutput: \n+------------+-----------------+\n| machine_id | processing_time |\n+------------+-----------------+\n| 0          | 0.894           |\n| 1          | 0.995           |\n| 2          | 1.456           |\n+------------+-----------------+\nExplanation: \nThere are 3 machines running 2 processes each.\nMachine 0's average time is ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894\nMachine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995\nMachine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456\n
\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 917, "dislikes": 79, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "{\"headers\":{\"Activity\":[\"machine_id\",\"process_id\",\"activity_type\",\"timestamp\"]},\"rows\":{\"Activity\":[[0,0,\"start\",0.712],[0,0,\"end\",1.52],[0,1,\"start\",3.14],[0,1,\"end\",4.12],[1,0,\"start\",0.55],[1,0,\"end\",1.55],[1,1,\"start\",0.43],[1,1,\"end\",1.42],[2,0,\"start\",4.1],[2,0,\"end\",4.512],[2,1,\"start\",2.5],[2,1,\"end\",5]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef get_average_time(activity: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"121.6K\", \"totalSubmission\": \"182K\", \"totalAcceptedRaw\": 121601, \"totalSubmissionRaw\": 181955, \"acRate\": \"66.8%\"}", "hints": [], "solution": { "id": "2091", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{\"headers\":{\"Activity\":[\"machine_id\",\"process_id\",\"activity_type\",\"timestamp\"]},\"rows\":{\"Activity\":[[0,0,\"start\",0.712],[0,0,\"end\",1.52],[0,1,\"start\",3.14],[0,1,\"end\",4.12],[1,0,\"start\",0.55],[1,0,\"end\",1.55],[1,1,\"start\",0.43],[1,1,\"end\",1.42],[2,0,\"start\",4.1],[2,0,\"end\",4.512],[2,1,\"start\",2.5],[2,1,\"end\",5]]}}", "metaData": "{\"mysql\": [\"Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float)\"], \"mssql\": [\"create table Activity (machine_id int, process_id int, activity_type varchar(15) not null check(activity_type in ('start', 'end')), timestamp float)\"], \"oraclesql\": [\"create table Activity (machine_id int, process_id int, activity_type varchar(15) not null check(activity_type in ('start', 'end')), timestamp float)\"], \"database\": true, \"name\": \"get_average_time\", \"pythondata\": [\"Activity = pd.DataFrame([], columns=['machine_id', 'process_id', 'activity_type', 'timestamp']).astype({'machine_id':'Int64', 'process_id':'Int64', 'activity_type':'object', 'timestamp':'Float64'})\"], \"postgresql\": [\"Create table If Not Exists Activity (machine_id int, process_id int, activity_type VARCHAR(30) CHECK (activity_type IN ('start', 'end')), timestamp float)\\n\"], \"database_schema\": {\"Activity\": {\"machine_id\": \"INT\", \"process_id\": \"INT\", \"activity_type\": \"ENUM('start', 'end')\", \"timestamp\": \"FLOAT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float)", "Truncate table Activity", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'start', '0.712')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'end', '1.52')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'start', '3.14')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'end', '4.12')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'start', '0.55')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'end', '1.55')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'start', '0.43')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'end', '1.42')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'start', '4.1')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'end', '4.512')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'start', '2.5')", "insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'end', '5')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"

MySQL 8.0.

\"], \"mssql\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"], \"pythondata\": [\"Pandas\", \"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0

\"], \"postgresql\": [\"PostgreSQL\", \"

PostgreSQL 16

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }