ORDER BY 或 GROUP BY 基于 SQL 服务器中的三列
ORDER BY or GROUP BY based on three columns in SQL Server
我有一个这样的table..
Priority | Amount | Case
P1 | 100 | 1
P1 | 200 | 2
P1 | 300 | 1
P3 | 400 | 3
我想先按 Priority
和 Amount
(降序)排序,然后按 Case
排序,看起来像这样。
Priority | Amount | Case
P1 | 300 | 1
P1 | 100 | 1
P1 | 200 | 2
P3 | 400 | 3
如果我使用 ORDER BY Priority, Amount DESC, Case
那么它 return 就是这个。其中 Case
未根据最高 Amount
值组合在一起。
Priority | Amount | Case
P1 | 300 | 1
P1 | 200 | 2
P1 | 100 | 1
P3 | 400 | 3
编辑:为清楚起见再添加一条记录:
Priority | Amount | Case
P1 | 100 | 1
P1 | 200 | 2
P1 | 300 | 1
P1 | 200 | 0 << New record
P3 | 400 | 3
这应该return为:
Priority | Amount | Case
P1 | 300 | 1
P1 | 100 | 1
P1 | 200 | 0
P1 | 200 | 2
P3 | 400 | 3
首先按 Priority
分组,其中按最高 Amount
排序,然后在 Amount
中按 Case
分组
我想你想要这个
select * from tablename ORDER BY Priority, [case] , Amount DESC
CREATE TABLE SHANKATABLE (PRIORITY VARCHAR(2), AMOUNT INT, [CASE] INT)
INSERT INTO SHANKATABLE VALUES('P1', 100 , 1)
INSERT INTO SHANKATABLE VALUES ('P1', 200 , 2)
INSERT INTO SHANKATABLE VALUES ('P1', 300 , 1)
INSERT INTO SHANKATABLE VALUES ('P3', 400 , 3)
要获得第一个结果,只需使用以下查询,
基于 Damien_The_Unbeliever 的回答,我只是更新我的回答。
SELECT PRIORITY,[CASE],AMOUNT
FROM (
SELECT PRIORITY,[CASE],AMOUNT,MAX(Amount) OVER(PARTITION BY [Case]) AS mAmount
FROM SHANKATABLE
) Temp
ORDER BY
[Priority],mAmount DESC,[Case],Amount DESC
标识符不能是SQL中的保留关键字,不能嵌入空格,也不能包含增补字符。
不符合这些规则的标识符必须被分隔。
例如,名为 2006 的属性被视为不规则标识符,因为它以数字开头,因此必须分隔为“2006”或 [2006]。像 y2006 这样的常规标识符可以在没有分隔符的情况下简单地引用为 y2006,或者它可以是可选的,带有分隔符。您可能不希望分隔常规标识符,因为分隔符往往会使代码混乱。
试试这个:-
select [PRIORITY],[amount],[case] from Table
order by [PRIORITY], [CASE] asc,AMOUNT desc
您需要使用窗口聚合找到每个 Case
中的 最高 金额,然后使用 that排序:
declare @t table ([Priority] varchar(19) not null,Amount int not null, [Case] int not null)
insert into @t ([Priority],Amount,[Case]) values
('P1',100,1),
('P1',200,2),
('P1',300,1),
('P1',200,0),
('P3',400,3)
select
*
from
(
select *,MAX(Amount) OVER (PARTITION BY [Case]) as mAmount
from @t
) t
order by [Priority],mAmount desc,[Case],Amount desc
结果:
Priority Amount Case mAmount
------------------- ----------- ----------- -----------
P1 300 1 300
P1 100 1 300
P1 200 0 200
P1 200 2 200
P3 400 3 400
此外,请考虑重命名您的一些列 - 我不得不将两个包含在 []
括号中,因为它们是保留字。通常最好完全避免使用保留字。
我有一个这样的table..
Priority | Amount | Case
P1 | 100 | 1
P1 | 200 | 2
P1 | 300 | 1
P3 | 400 | 3
我想先按 Priority
和 Amount
(降序)排序,然后按 Case
排序,看起来像这样。
Priority | Amount | Case
P1 | 300 | 1
P1 | 100 | 1
P1 | 200 | 2
P3 | 400 | 3
如果我使用 ORDER BY Priority, Amount DESC, Case
那么它 return 就是这个。其中 Case
未根据最高 Amount
值组合在一起。
Priority | Amount | Case
P1 | 300 | 1
P1 | 200 | 2
P1 | 100 | 1
P3 | 400 | 3
编辑:为清楚起见再添加一条记录:
Priority | Amount | Case
P1 | 100 | 1
P1 | 200 | 2
P1 | 300 | 1
P1 | 200 | 0 << New record
P3 | 400 | 3
这应该return为:
Priority | Amount | Case
P1 | 300 | 1
P1 | 100 | 1
P1 | 200 | 0
P1 | 200 | 2
P3 | 400 | 3
首先按 Priority
分组,其中按最高 Amount
排序,然后在 Amount
中按 Case
我想你想要这个
select * from tablename ORDER BY Priority, [case] , Amount DESC
CREATE TABLE SHANKATABLE (PRIORITY VARCHAR(2), AMOUNT INT, [CASE] INT)
INSERT INTO SHANKATABLE VALUES('P1', 100 , 1)
INSERT INTO SHANKATABLE VALUES ('P1', 200 , 2)
INSERT INTO SHANKATABLE VALUES ('P1', 300 , 1)
INSERT INTO SHANKATABLE VALUES ('P3', 400 , 3)
要获得第一个结果,只需使用以下查询,
基于 Damien_The_Unbeliever 的回答,我只是更新我的回答。
SELECT PRIORITY,[CASE],AMOUNT
FROM (
SELECT PRIORITY,[CASE],AMOUNT,MAX(Amount) OVER(PARTITION BY [Case]) AS mAmount
FROM SHANKATABLE
) Temp
ORDER BY
[Priority],mAmount DESC,[Case],Amount DESC
标识符不能是SQL中的保留关键字,不能嵌入空格,也不能包含增补字符。
不符合这些规则的标识符必须被分隔。
例如,名为 2006 的属性被视为不规则标识符,因为它以数字开头,因此必须分隔为“2006”或 [2006]。像 y2006 这样的常规标识符可以在没有分隔符的情况下简单地引用为 y2006,或者它可以是可选的,带有分隔符。您可能不希望分隔常规标识符,因为分隔符往往会使代码混乱。
试试这个:-
select [PRIORITY],[amount],[case] from Table
order by [PRIORITY], [CASE] asc,AMOUNT desc
您需要使用窗口聚合找到每个 Case
中的 最高 金额,然后使用 that排序:
declare @t table ([Priority] varchar(19) not null,Amount int not null, [Case] int not null)
insert into @t ([Priority],Amount,[Case]) values
('P1',100,1),
('P1',200,2),
('P1',300,1),
('P1',200,0),
('P3',400,3)
select
*
from
(
select *,MAX(Amount) OVER (PARTITION BY [Case]) as mAmount
from @t
) t
order by [Priority],mAmount desc,[Case],Amount desc
结果:
Priority Amount Case mAmount
------------------- ----------- ----------- -----------
P1 300 1 300
P1 100 1 300
P1 200 0 200
P1 200 2 200
P3 400 3 400
此外,请考虑重命名您的一些列 - 我不得不将两个包含在 []
括号中,因为它们是保留字。通常最好完全避免使用保留字。