简体   繁体   中英

Select Query Min and Max in one column based on other column value

I have an employee table with a name, salary, HRA, DA,Status

I want to print these salary, HRA, DA,Status columns with only 2 records in them, the name of my highest and lowest salary,HRA,DA based on Status Column where Active from employee.

It should look something like this:

在此处输入图像描述

SELECT 
    salary, MIN(salary), MAX(salary) AND 
    HRA, MIN(HRA), MAX(HRA) AND 
    DA, MIN(HRA), MAX(HRA) AND Where Status = 'Active'FROM employee

you need to do so:

SELECT 
     MIN(salary) salary,  MIN(HRA) HRA, MIN(DA) DA
FROM employee
Where Status = 'Active'
UNION
SELECT 
     MAX(salary),  MAX(HRA), MAX(DA)
FROM employee
Where Status = 'Active'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM