简体   繁体   English

MS SQL Server多列独立排序

[英]MS SQL Server multiple column sort independently

So to get things out of the way, due to structure i'm left in a situation of trying to sort some columns of data in SQL in a way that is evading me. 因此,为了避免问题,由于结构的原因,我不得不尝试以逃避我的方式对SQL中的某些数据列进行排序。 The problem is that i need multiple sets of 2 columns sorted independently, For example i have something like this: 问题是我需要多组2列独立排序,例如,我有这样的东西:

  Name | Val1 | Name | Val2 | Name | Val3 A | 2 | A | 1 | A | 3 B | 1 | B | 3 | B | 2 C | 3 | C | 2 | C | 1 

and i need the table to be sorted by the highest of each value: 我需要按每个值中的最高值对表格进行排序:

  Name | Val1 | Name | Val2 | Name | Val3 C | 3 | B | 3 | A | 3 A | 2 | C | 2 | B | 2 B | 1 | A | 1 | C | 1 

I don't seem to know how to organise using ROW_NUMBER() and various other things i have done through long searches being able to separate out individual columns for ordering but i don't know how i can keep two linked while the others sort independently, Can anyone help? 我似乎不知道如何使用ROW_NUMBER()和我通过长时间搜索完成的各种其他事情来组织工作,这些事情可以分离出单独的列进行排序,但是我不知道如何保持两个链接而其他链接独立排序,谁能帮忙? EDIT: The Data is Extrapolated from one table after calculations had been done for their values. 编辑:在对数据进行计算之后,从一张表中推断出数据。 So say i have my table of: 所以说我有以下表格:

  Name | Val1 | Val2 | Val3 | A | 2 | 1 | 3 | B | 1 | 3 | 2 | C | 3 | 2 | 1 | 

The values are names specifically are just used for example, but are wildly Differing values. 这些值是专门用于命名的名称,例如,但值却大相径庭。 So from that table of final results i need to get the results in the format that the name with the highest value will be on top for each individual value SELECT Name AS N1, Val1, Name As N2, Val2 etc EDIT: Example: 因此,从最终结果表中,我需要以以下格式获取结果:对于每个单独的值,具有最高值的名称将位于顶部。选择名称AS N1,Val1,名称As N2,Val2等编辑:示例:

  Name1|Units|Name2|Units| Name3|Units AF |218 |AF |0.83 | AF |1.04 AD |172 |AD |0.49 | AD |1.05 AF |116 |AF |0.87 | AF |1.06 AF |324 |AF |0.84 | AF |1.10 

If I understand your question correctly, consider the following approach: 如果我正确理解您的问题,请考虑以下方法:

CREATE TABLE #NameValue (
    Name varchar(10),
    Val1 int,
    Val2 int,
    Val3 int
)

INSERT INTO #NameValue 
VALUES
    ('A', 102, 201, 303),
    ('B', 101, 203, 302),
    ('C', 103, 202, 301);

WITH nv1 AS (
    SELECT Name, Val1, ROW_NUMBER() OVER (ORDER BY Val1 DESC) AS RN1
    FROM #NameValue
),
nv2 AS (
    SELECT Name, Val2, ROW_NUMBER() OVER (ORDER BY Val2 DESC) AS RN2
    FROM #NameValue
),
nv3 AS (
    SELECT Name, Val3, ROW_NUMBER() OVER (ORDER BY Val3 DESC) AS RN3
    FROM #NameValue
)
SELECT 
    nv1.Name AS Name1, nv1.Val1, 
    nv2.Name AS Name2, nv2.Val2,
    nv3.Name AS Name3, nv3.Val3
FROM nv1
LEFT JOIN nv2 ON (nv1.RN1 = nv2.RN2)
LEFT JOIN nv3 ON (nv1.RN1 = nv3.RN3)

Output: 输出:

Name1   Val1    Name2   Val2    Name3   Val3
C       103     B       203     A       303
A       102     C       202     B       302
B       101     A       201     C       301

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

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