返回介绍

solution / 1900-1999 / 1907.Count Salary Categories / README_EN

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

1907. Count Salary Categories

中文文档

Description

Table: Accounts

+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id  | int  |
| income    | int  |
+-------------+------+
account_id is the primary key (column with unique values) for this table.
Each row contains information about the monthly income for one bank account.

 

Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:

  • "Low Salary": All the salaries strictly less than $20000.
  • "Average Salary": All the salaries in the inclusive range [$20000, $50000].
  • "High Salary": All the salaries strictly greater than $50000.

The result table must contain all three categories. If there are no accounts in a category, return 0.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
| 3      | 108939 |
| 2      | 12747  |
| 8      | 87709  |
| 6      | 91796  |
+------------+--------+
Output: 
+----------------+----------------+
| category     | accounts_count |
+----------------+----------------+
| Low Salary   | 1        |
| Average Salary | 0        |
| High Salary  | 3        |
+----------------+----------------+
Explanation: 
Low Salary: Account 2.
Average Salary: No accounts.
High Salary: Accounts 3, 6, and 8.

Solutions

Solution 1: Temporary Table + Grouping + Left Join

We can first create a temporary table containing all salary categories, and then count the number of bank accounts for each salary category. Finally, we use a left join to connect the temporary table with the result table to ensure that the result table contains all salary categories.

# Write your MySQL query statement below
WITH
  S AS (
    SELECT 'Low Salary' AS category
    UNION
    SELECT 'Average Salary'
    UNION
    SELECT 'High Salary'
  ),
  T AS (
    SELECT
      CASE
        WHEN income < 20000 THEN "Low Salary"
        WHEN income > 50000 THEN 'High Salary'
        ELSE 'Average Salary'
      END AS category,
      COUNT(1) AS accounts_count
    FROM Accounts
    GROUP BY 1
  )
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
FROM
  S
  LEFT JOIN T USING (category);

Solution 2: Filtering + Merging

We can filter out the number of bank accounts for each salary category separately, and then merge the results. Here, we use UNION to merge the results.

# Write your MySQL query statement below
SELECT 'Low Salary' AS category, IFNULL(SUM(income < 20000), 0) AS accounts_count FROM Accounts
UNION
SELECT
  'Average Salary' AS category,
  IFNULL(SUM(income BETWEEN 20000 AND 50000), 0) AS accounts_count
FROM Accounts
UNION
SELECT 'High Salary' AS category, IFNULL(SUM(income > 50000), 0) AS accounts_count FROM Accounts;

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

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

发布评论

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