简体   繁体   English

在SQL中将Where子句应用于Order by

[英]Applying Where clause for Order by in SQL

Would like to sort the following data the way allows in the order of choice of keywords based on like clause: 想要按照允许的方式按照以下条件对以下数据进行排序:

Data in table: 表中数据:

EmpId EmpLotusNotes
10001 Amit B/India
20002 Bharat C/India
30003 Kyo Jun/Japan
40004 Jee Lee/China
50005 Xavier K/USA

Data to be presented/sorted based on certain country order (Japan, China, India, USA): 根据某些国家/地区的顺序显示/分类的数据(日本,中国,印度,美国):

EmpId EmpLotusNotes
30003 Kyo Jun/Japan
40004 Jee Lee/China
10001 Amit B/India
20002 Bharat C/India
50005 Xavier K/USA

Note: I cannot create another table that holds the country order or any other change. 注意:我无法创建包含国家/地区订单或任何其他更改的另一个表。

This should make a trick: 这应该是个窍门:

SELECT
   EmpId, EmpLotusNotes
FROM 
   dbo.Table
ORDER BY
   CASE 
      WHEN EmpLotusNotes LIKE '%Japan' THEN 1
      WHEN EmpLotusNotes LIKE '%China' THEN 2 
      WHEN EmpLotusNotes LIKE '%India' THEN 3
      WHEN EmpLotusNotes LIKE '%USA' THEN 4
   END

Perhaps: 也许:

SELECT
   EmpId, EmpLotusNotes
FROM 
   dbo.Table
ORDER BY
   CASE WHEN EmpLotusNotes LIKE '%Japan' THEN 0 ELSE 1 END,
   CASE WHEN EmpLotusNotes LIKE '%China' THEN 0 ELSE 1 END,
   CASE WHEN EmpLotusNotes LIKE '%India' THEN 0 ELSE 1 END,
   CASE WHEN EmpLotusNotes LIKE '%USA' THEN 0 ELSE 1 END

and here's the DEMO 这是演示

The problem is the table violates first normal form, EmpLotusNotes should not contain the name of an employee and the country, presumably the country they work in. 问题是表格违反了第一个范式,EmpLotusNotes不应包含雇员的姓名和所在国家(大概是他们工作的国家)。

You should challenge the reasons why you are not allowed to clean up the structure and the data. 您应该挑战不允许清理结构和数据的原因。

See https://www.google.com.au/search?q=sql+first+normal+form+atomic 请参阅https://www.google.com.au/search?q=sql+first+normal+form+atomic

The answer, if you still cannot normalise the database after challenging, is create a query for countries, create a query to split the data in the first table into first normal form, then join the two. 如果您在进行挑战后仍无法规范化数据库,那么答案是创建一个查询国家/地区的查询,创建一个查询以将第一个表中的数据拆分为第一个普通形式,然后将两者合并。

An example that works for mysql follows, for MS SQL you would use CHARINDEX instead of INSTR and substring instead of substr. 下面是一个适用于mysql的示例,对于MS SQL,您将使用CHARINDEX而不是INSTR和子字符串而不是substr。

select employeesWithCountries.*
, countries.sort 
from (
    select empId, empLotusNotes, substr( empLotusNotes, afterStartOfDelimiter ) country from (
        select empId
        , empLotusNotes
        , INSTR( empLotusNotes, '/' ) + 1 as afterStartOfDelimiter 
        from EmployeesLotusNotes
    ) employees
) employeesWithCountries
inner join (
    SELECT 'Japan' as country, 1 as sort
    union
    SELECT 'China' as country, 2 as sort
    union
    SELECT 'India' as country, 3 as sort
    union
    SELECT 'USA' as country, 4 as sort
) countries
on employeesWithCountries.country = countries.country
order by countries.sort, employeesWithCountries.empLotusNotes

Results. 结果。

30003    Kyo Jun/Japan   Japan    1
40004    Jee Lee/China   China    2
10001    Amit B/India    India    3
20002    Bharat C/India  India    3
50005    Xavier K/USA    USA      4

You can use a Common Table Expression to process the raw data first and do your filtering and/or ordering on the processed data later. 您可以使用“公用表表达式”首先处理原始数据,然后再对处理后的数据进行过滤和/或排序。 Something like below. 像下面这样。 The function in myCol can be changed with any kind of function, even a CASE clause. myCol中的函数可以使用任何类型的函数进行更改,甚至包括CASE子句。

WITH T as 
(SELECT EmpId, EmpLotusNotes, SOMEFUNCTION(EmpLotusNotes) as myCol
 FROM Table1
)
SELECT *
FROM T 
WHERE myCol = XXX
ORDER BY myCol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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