下面是一個例子:
桌子
| 姓名 | 薪水 |
|---|---|
| 一種 | 1000 |
| 乙 | 500 |
| C | 400 |
| D | 100 |
輸出
| 姓名 | 最高薪水 | 姓名 | 工資_最低 |
|---|---|---|---|
| 一種 | 1000 | D | 100 |
| 乙 | 500 | C | 400 |
因此,最高和最低工資應該被映射并放在一行中,并且輸出應該是多行而不是單行映射高低工資,直到表的資料結束。
我能夠獲得最大值和最小值,第二個最大值和第二個最小值等等,并且一次只能獲得一行,但這不是我想要的。
我們可以使用的任何回圈。我正在使用 MySQL - mycompiler.io 在線編譯器。
更新:我試過這種方式 -
Select E1.salary as MIN, E2.salary as MAX
from employees E1, employees E2 where
E1.salary < (select max(salary) from employees) and
E2.salary < (select min(salary) from employees);
并得到這個輸出-
輸出
| 最小 | 最大限度 |
|---|---|
| 500 | 1000 |
| 400 | 1000 |
| 100 | 1000 |
| 500 | 500 |
| 400 | 500 |
| 100 | 500 |
| 500 | 400 |
| 400 | 400 |
| 100 | 400 |
猜猜回圈作業,但所有額外的記錄都必須去,輸出應該是-
| 最小 | 最大限度 |
|---|---|
| 100 | 1000 |
| 400 | 500 |
存在于串列中。我哪里錯了
uj5u.com熱心網友回復:
您可以在公共表運算式中計算升序和降序,然后自聯接:
with ranked_employees as (
select name, salary,
row_number() over w_asc row_asc,
row_number() over w_desc row_desc
from employees
window
w_asc as (order by salary, id),
w_desc as (order by salary desc, id desc)
)
select d.name, d.salary, a.name, a.salary
from ranked_employees d
inner join ranked_employees a on a.row_asc=d.row_desc and d.row_asc >= d.row_desc
該d.row_asc >= d.row_desc條件使得它停在中點。
請注意,您需要對 id 或其他一些唯一欄位以及薪水進行排序,以保證升序和降序計數之間的順序一致。
uj5u.com熱心網友回復:
快速回答:
SELECT
tdesc.name AS highname,
tdesc.salary AS hightsalary,
tasc.name AS lowname,
tasc.salary AS lowsalary
FROM (
SELECT
ROW_NUMBER() OVER (ORBER BY salary, name) rank1,
name,
salary
FROM table
ORDER BY salary, name ASC
) AS tasc
INNER JOIN (
SELECT
ROW_NUMBER() OVER (ORDER BY salary, name) rank2,
name,
salary
FROM table
ORDER BY salary, name DESC
) AS tdesc
ON tdesc.rank2 = tasc.rank1
WHERE tdesc.salary >= tasc.salary
解釋:
作為@akina評論說,我們要加入到副本基地以相反的順序表:
為此,我們將首先宣告 2 個行計數器:rank1 和 rank2。
編輯:MySQL 支持ROW_NUMBER功能,允許相同的目的。
第一個子查詢創建一個臨時表 tasc ,其中包含來自基表的所有值,沿著“訂單 ID”,按工資 ASC 排序。
任務:
| 等級1 | 姓名 | 薪水 |
|---|---|---|
| 1 | 一種 | 1000 |
| 2 | 乙 | 500 |
| 3 | C | 400 |
| 4 | D | 100 |
第二個子查詢將執行相同的操作,但順序相反。
tdesc :
| 等級2 | 姓名 | 薪水 |
|---|---|---|
| 1 | D | 100 |
| 2 | C | 400 |
| 3 | 乙 | 500 |
| 4 | 一種 | 1000 |
現在,我們tasc.rank1 = tdesc.rank2使用一些別名加入我們的表。
連接表:
| 名門望族 | 高薪 | 低名 | 低薪 |
|---|---|---|---|
| 一種 | 1000 | D | 100 |
| 乙 | 500 | C | 400 |
| C | 400 | 乙 | 500 |
| D | 100 | 一種 | 1000 |
As you can see, all rows are now duplicated, the second half has also irrelevant values, we must filter it out whith the where condition WHERE tdesc.salary >= tasc.salary.
Final result:
| highname | highsalary | lowname | lowsalary |
|---|---|---|---|
| A | 1000 | D | 100 |
| B | 500 | C | 400 |
IN THE CASE OF ODD NUMBER OF ROWS:
The or equal in the >= where clause will make sure you won't loose data, and the last row of the result will contain twice the datas for the median salary.
| highname | highsalary | lowname | lowsalary |
|---|---|---|---|
| A | 1000 | D | 100 |
| B | 500 | C | 400 |
| E | 450 | E | 450 |
IN THE CASE OF SAME SALARY FOR MULTIPLE PEOPLE:
The or equal will once again make sure you won't loose any data. In addition, the ORDER BY salary, name will make sure you have different names in the rows:
| highname | highsalary | lowname | lowsalary |
|---|---|---|---|
| A | 1000 | D | 100 |
| B | 500 | C | 400 |
| E | 450 | F | 450 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332694.html
