Skip to content

Latest commit

 

History

History
105 lines (81 loc) · 2 KB

File metadata and controls

105 lines (81 loc) · 2 KB
comments difficulty edit_url tags
true
中等
数据库

English Version

题目描述

表:Data

+-------------+------+
| Column Name | Type |
+-------------+------+
| first_col   | int  |
| second_col  | int  |
+-------------+------+
该表可能包含重复数据。

 

编写解决方案,使:

  • first_col 按照 升序 排列。
  • second_col 按照 降序 排列。

返回的结果格式如下。

 

示例 1:

输入:
Data 表:
+-----------+------------+
| first_col | second_col |
+-----------+------------+
| 4         | 2          |
| 2         | 3          |
| 3         | 1          |
| 1         | 4          |
+-----------+------------+
输出:
+-----------+------------+
| first_col | second_col |
+-----------+------------+
| 1         | 4          |
| 2         | 3          |
| 3         | 2          |
| 4         | 1          |
+-----------+------------+

解法

方法一

MySQL

# Write your MySQL query statement below
WITH
    S AS (
        SELECT
            first_col,
            ROW_NUMBER() OVER (ORDER BY first_col) AS rk
        FROM Data
    ),
    T AS (
        SELECT
            second_col,
            ROW_NUMBER() OVER (ORDER BY second_col DESC) AS rk
        FROM Data
    )
SELECT first_col, second_col
FROM
    S
    JOIN T USING (rk);