SQL order by ASC Second(value) space ASC First(value) 在聚合 SQL 查询的同一列中

Posted

技术标签:

【中文标题】SQL order by ASC Second(value) space ASC First(value) 在聚合 SQL 查询的同一列中【英文标题】:SQL order by ASC Second(value) space ASC First(value) in same column in aggregate SQL query 【发布时间】:2022-01-01 16:36:47 【问题描述】:

我想按列名称排序,该列名称在同一列中具有 "Value1" Space "Value2" 之类的值。 例如。

Column1
Value1 value2
NSPP02 2111
NSPP02 2110
NSPP01 2111

我想先按 value2 排序,然后按 value1 排序。 ASC 按值 2,然后按值 1 ASC。

我使用聚合如下:

select 
    pdc.semester as Semester,
    count(pdc.CNIC) as TotalRegistrations,
    count(s.studentid) as TotalRegistered,
    count(case when r.Grade is not null then 'Appeared' end) as Appeared,
    count(case when r.Grade='f' then 'fail' end) as Failed,
    count(case when r.grade <>'f' then 'pass' end) as Passed
from
    PDC_PreRegistration pdc 
left join  
    Students s on pdc.Semester = s.CurSemester and s.nic = pdc.CNIC
left join 
    studentresultnet r on s.studentid = r.studentid and s.CurSemester = r.Semester 
left join 
    semester se on se.Semester = r.Semester
where 
    pdc.semester in (select CurSemester from students where batch = 'PD2110') 
group by 
    pdc.Semester
order by  
    semester desc

本次查询的结果如下:

Semester TotalRegistrations TotalRegistered Appeared Failed Passed
NSPP02 2111 121 81 0 0 0
NSPP02 2110 37 35 24 1 23
NSPP01 2111 76 42 0 0 0

我希望结果是这样的:

Semester TotalRegistrations TotalRegistered Appeared Failed Passed
NSPP02 2110 37 35 24 1 23
NSPP01 2111 76 42 0 0 0
NSPP02 2111 121 81 0 0 0

按值 2 进行 ASC,然后按值 1 进行 ASC。

学期列的示例序列如下:

NSPP01 2110 NSPP02 2110 NSPP01 2111 NSPP02 2111 NSPP01 2112 NSPP02 2112 NSPP01 2201 NSPP02 2201 NSPP01 2202 NSPP02 2202 NSPP01 2203 NSPP02 2203 等等

查看此屏幕截图:

Screenshot of query and its result

【问题讨论】:

如果您对单独的部分感兴趣,将两个值存储在一列中是个坏主意。相应地改变你的桌子设计,你已经解决了这个问题。 【参考方案1】:

我不确定您的原因或该列背后的逻辑,但理想情况下,您应该将它们作为 2 个单独的列。它使这样的事情变得更容易。

话虽如此,我认为有两种方法可以解决这个问题。

如果您的 2 个值的长度永远不会改变,即值 NSPP02 将始终包含 5 个字符,而 2203 值将始终包含 4 个字符,那么您可以执行以下操作:

SELECT LEFT(COLUMN_1,5) AS VALUE 1
SELECT RIGHT(COLUMN_1,4) AS VALUE 2
ORDER BY VALUE 2, VALUE 1

或者,如果每个值中的字符数有增加的空间,那么您将需要使用类似 SUBSTRING 函数的东西。有一些不错的细节here:

【讨论】:

【参考方案2】:

您实际上可以先拆分学期列和SORT,然后您可以抑制这些辅助列以获得所需的输出:

SELECT Semester, TotalRegistrations, TotalRegistered, Appeared, Failed, Passed
FROM(
SELECT pdc.semester as Semester,count(pdc.CNIC) as 
       TotalRegistrations,count(s.studentid) as TotalRegistered,
       count(case when r.Grade is not null then 'Appeared' end) as Appeared,
       count(case when r.Grade='f' then 'fail' end) as Failed,
       count(case when r.grade <>'f' then 'pass' end) as Passed,
       TRIM(LEFT(pdc.semester, CHARINDEX(' ', pdc.semester) - 1)) as frst, 
       TRIM(RIGHT(pdc.semester, LEN(pdc.semester) - CHARINDEX(' ', pdc.semester))) as sec
FROM PDC_PreRegistration pdc 
LEFT JOIN Students s on pdc.Semester=s.CurSemester and s.nic=pdc.CNIC
LEFT JOIN studentresultnet r on s.studentid=r.studentid and s.CurSemester=r.Semester 
LEFT JOIN semester se on se.Semester=r.Semester
WHERE pdc.semester
IN (select CurSemester from students where  batch='PD2110')
GROUP BY pdc.Semester
)
ORDER BY sec, frst

【讨论】:

以上是关于SQL order by ASC Second(value) space ASC First(value) 在聚合 SQL 查询的同一列中的主要内容,如果未能解决你的问题,请参考以下文章

sql查询中的“ORDER BY meta_value ASC”未正确排序价格

sql 第2行显示如何GROUP BY Last Name,然后ORDER BY两件事 - 首先是COUNT(OrderID),然后是LastName,在ASC ord中排序

SQL order by的用法

SQL里面的排序语句desc和ASC有啥区别

sql里的排序倒序的命令是order by啥

SQL中order by;group up;like;关联查询join on的用法