返回介绍

solution / 1900-1999 / 1990.Count the Number of Experiments / README_EN

发布于 2024-06-17 01:03:12 字数 3563 浏览 0 评论 0 收藏 0

1990. Count the Number of Experiments

中文文档

Description

Table: Experiments

+-----------------+------+
| Column Name   | Type |
+-----------------+------+
| experiment_id   | int  |
| platform    | enum |
| experiment_name | enum |
+-----------------+------+
experiment_id is the column with unique values for this table.
platform is an enum (category) type of values ('Android', 'IOS', 'Web').
experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').
This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.

 

Write a solution to report the number of experiments done on each of the three platforms for each of the three given experiments. Notice that all the pairs of (platform, experiment) should be included in the output including the pairs with zero experiments.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input:
Experiments table:
+---------------+----------+-----------------+
| experiment_id | platform | experiment_name |
+---------------+----------+-----------------+
| 4       | IOS    | Programming   |
| 13      | IOS    | Sports      |
| 14      | Android  | Reading     |
| 8       | Web    | Reading     |
| 12      | Web    | Reading     |
| 18      | Web    | Programming   |
+---------------+----------+-----------------+
Output: 
+----------+-----------------+-----------------+
| platform | experiment_name | num_experiments |
+----------+-----------------+-----------------+
| Android  | Reading     | 1         |
| Android  | Sports      | 0         |
| Android  | Programming   | 0         |
| IOS    | Reading     | 0         |
| IOS    | Sports      | 1         |
| IOS    | Programming   | 1         |
| Web    | Reading     | 2         |
| Web    | Sports      | 0         |
| Web    | Programming   | 1         |
+----------+-----------------+-----------------+
Explanation: 
On the platform "Android", we had only one "Reading" experiment.
On the platform "IOS", we had one "Sports" experiment and one "Programming" experiment.
On the platform "Web", we had two "Reading" experiments and one "Programming" experiment.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  P AS (
    SELECT 'Android' AS platform
    UNION
    SELECT 'IOS'
    UNION
    SELECT 'Web'
  ),
  Exp AS (
    SELECT 'Reading' AS experiment_name
    UNION
    SELECT 'Sports'
    UNION
    SELECT 'Programming'
  ),
  T AS (
    SELECT *
    FROM
      P,
      Exp
  )
SELECT platform, experiment_name, COUNT(experiment_id) AS num_experiments
FROM
  T AS t
  LEFT JOIN Experiments USING (platform, experiment_name)
GROUP BY 1, 2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文