sql中的员工姓名排序时,如何优先考虑单个员工?
How to give priority to a single employee while sorting the employee name in sql?
我 table 喜欢 TB1:
Emp_name Dept_name salary
Girish BB 20000
Bhanu AA 10000
Mahesh CC 10000
Seema YY 30000
我需要的输出是:
Emp_name Dept_name salary
Mahesh CC 10000
Bhanu AA 10000
Girish BB 20000
Seema YY 30000
这里我所做的是优先'Mahesh',其余所有员工都按升序排列。
我试过这个查询:
SELECT *
FROM Employee
ORDER BY CASE
WHEN Emp_name LIKE '%Mahesh%' THEN 1
WHEN Emp_name LIKE '%' THEN 2
ELSE 3
end;
感谢您的宝贵时间。
只需使用正确的 ORDER BY
子句:
示例数据:
SELECT *
INTO Employee
FROM (VALUES
('Girish', 'BB', 20000),
('Bhanu', 'AA', 10000),
('Mahesh', 'CC', 10000),
('Seema', 'YY', 30000)
) Employee (Emp_name, Dept_name, Salary)
声明:
SELECT *
FROM Employee
ORDER BY
CASE
WHEN Emp_name LIKE '%Mahesh%' THEN 1
ELSE 2
END,
Emp_name ASC
我的解决方案:
SELECT Emp_name, Dept_name, Salary FROM (
select T.*,
CASE
WHEN Emp_name = 'Mahesh' THEN
0
ELSE
1
END SORT1
from (VALUES
('Girish', 'BB', 20000),
('Bhanu', 'AA', 10000),
('Mahesh', 'CC', 10000),
('Seema', 'YY', 30000)
) T (Emp_name, Dept_name, Salary)) TT
ORDER BY TT.SORT1,TT.Emp_name
SELECT *
FROM Employee
ORDER BY
CASE
WHEN Emp_name Like '%Mahesh%' THEN 1
ELSE 2
END,
Dept_name ASC, salary ASC
运行 此查询在 TB1 table
CREATE TABLE TB2
SELECT * FROM TB1 ORDER BY salary ASC;
TB2 在何处创建运行 此查询
SELECT * FROM TB2 ORDER BY Dept_name='cc' DESC;
我 table 喜欢 TB1:
Emp_name Dept_name salary
Girish BB 20000
Bhanu AA 10000
Mahesh CC 10000
Seema YY 30000
我需要的输出是:
Emp_name Dept_name salary
Mahesh CC 10000
Bhanu AA 10000
Girish BB 20000
Seema YY 30000
这里我所做的是优先'Mahesh',其余所有员工都按升序排列。
我试过这个查询:
SELECT *
FROM Employee
ORDER BY CASE
WHEN Emp_name LIKE '%Mahesh%' THEN 1
WHEN Emp_name LIKE '%' THEN 2
ELSE 3
end;
感谢您的宝贵时间。
只需使用正确的 ORDER BY
子句:
示例数据:
SELECT *
INTO Employee
FROM (VALUES
('Girish', 'BB', 20000),
('Bhanu', 'AA', 10000),
('Mahesh', 'CC', 10000),
('Seema', 'YY', 30000)
) Employee (Emp_name, Dept_name, Salary)
声明:
SELECT *
FROM Employee
ORDER BY
CASE
WHEN Emp_name LIKE '%Mahesh%' THEN 1
ELSE 2
END,
Emp_name ASC
我的解决方案:
SELECT Emp_name, Dept_name, Salary FROM (
select T.*,
CASE
WHEN Emp_name = 'Mahesh' THEN
0
ELSE
1
END SORT1
from (VALUES
('Girish', 'BB', 20000),
('Bhanu', 'AA', 10000),
('Mahesh', 'CC', 10000),
('Seema', 'YY', 30000)
) T (Emp_name, Dept_name, Salary)) TT
ORDER BY TT.SORT1,TT.Emp_name
SELECT *
FROM Employee
ORDER BY
CASE
WHEN Emp_name Like '%Mahesh%' THEN 1
ELSE 2
END,
Dept_name ASC, salary ASC
运行 此查询在 TB1 table
CREATE TABLE TB2
SELECT * FROM TB1 ORDER BY salary ASC;
TB2 在何处创建运行 此查询
SELECT * FROM TB2 ORDER BY Dept_name='cc' DESC;