简体   繁体   English

仅按两个不同列中的一个排序

[英]Order by only one, of two distinct columns

I am selecting ColA and ColB in the same statement: 我在同一个声明中选择了ColA和ColB:

select distinct ColA, ColB, EDITABLE from TableA;

I would like to use ORDER BY lower(ColA) , because I would like to sort ColA alphabetically, without taking capitalization into consideration. 我想使用ORDER BY lower(ColA) ,因为我想按字母顺序对ColA进行排序,而不考虑大小写。 ColB does not need to be affect in this sorting and I would like to only sort ColA (which needs to be distinct because there are many instances of the same value in ColA). ColB不需要对此排序产生影响,我只想对ColA进行排序(需要区分ColA,因为ColA中有许多相同值的实例)。

I tried 我试过了

select distinct ColA, ColB, EDITABLE
from TableA
ORDER BY lower(ColA)

and also saw this question concerning distinct and order by but when I tried 并且当我尝试时,也看到了关于明显和秩序的这个问题

select distinct ColA, ColB, lower(ColA), EDITABLE
from TableA
GROUP BY ColA
ORDER BY lower(ColA) ASC, ColA

I was unable to run the statement above. 我无法执行上述声明。 I am very new at SQL and would love some tips on why this didn't work and help on what I can improve this statement on 我是SQL的新手,并且会喜欢为什么这不起作用的一些提示,并帮助我可以改进这个声明

Mention all the columns in the Group By that you are Select ing 提及所有在列Group By ,你是Select ING

In SQLServer, it is like: 在SQLServer中,它就像:

select distinct ColA, ColB, lower(ColA)
from TableA
GROUP BY ColA, ColB, lower(ColA)
ORDER BY lower(ColA) ASC

Your referenced question does not apply to your question. 您引用的问题不适用于您的问题。 In that question, there is another column used for ordering, one not in the original select distinct . 在该问题中,还有另一列用于排序,一列不在原始select distinct In your case, the order by is using a function on one of the original columns. 在您的情况下, order by使用其中一个原始列上的函数。 There should be no need to repeat the expression in the SELECT . 应该不需要在SELECT重复表达式。

You can also use group by : 您还可以使用group by

select ColA, ColB, EDITABLE
from TableA
group by ColA, ColB, EDITABLE
order by lower(ColA);

This is your query: 这是您的查询:

select distinct ColA, ColB, lower(ColA), EDITABLE
from TableA
GROUP BY ColA
ORDER BY lower(ColA) ASC, ColA

And this is what it does: 这就是它的作用:

1.   FROM clause: select rows from TableA.
2.   GROUP BY clause: Aggregate rows so as to get one row per ColA.
3.   SELECT clause:
3.1.   Show ColA. This is okay, ColA is what you group by.
3.2.   Show ColB. Which? There can be diferent ColB per ColA. This produces an error.
3.3.   Show lower(ColA). That is okay. You group by ColA, so lower(ColA) is also known.
3.4.   Show EDITABLE. Again: which? There can be diferent EDITABLE per ColA.
3.5.   DISTINCT: remove duplicate rows. This is superfluous, because there cannot be
       duplicate rows. They all have different ColA.
4.   ORDER BY clause:
4.1.   Sort by lower(ColA), so you have 'John' and 'john' together.
4.2.   Sort by ColA. This tells the DBMS whther to put 'John' or 'john' first.

I hope this explains how a query gets executed, what GROUP BY does and what is allowed in the SELECT clause. 我希望这能解释查询是如何执行的, GROUP BY执行的操作以及SELECT子句中允许的内容。 DISTINCT is very often a sign for a badly written query. DISTINCT通常是编写错误查询的标志。 Here it's merely superfluous, but quite often it is used as some defense against poor joins leading to duplicate rows. 在这里它只是多余的,但通常它被用作防止导致重复行的不良连接的一些防御。 Whenever you see SELECT DISTINCT ask yourself what makes it necessary. 每当你看到SELECT DISTINCT问问自己是什么使它成为必要。

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

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