SQL order by ASC Second(value) space ASC First(value) in aggregate SQL query
SQL order by ASC Second(value) space ASC First(value) in same column in aggregate SQL query
我想按 "Value1" Space "Value2" 等值的列名排序 同一列。 例如
Column1
Value1 value2
NSPP02 2111
NSPP02 2110
NSPP01 2111
我想先按value2再按value1排序。
按值 2 升序,然后按值 1 升序。
我使用聚合如下:
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 升序,然后按值 1 升序。
学期栏的示例序列如下:
- 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
我不确定您的理由或列背后的逻辑,但理想情况下,您将它们作为 2 个单独的列。它使这样的事情变得更容易。
话虽如此,我认为有两种方法可以解决这个问题。
如果您的 2 个值的长度永远不会改变 i.e.the 值 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 函数的功能。有一些不错的细节:
您实际上可以先拆分学期列,然后 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
我想按 "Value1" Space "Value2" 等值的列名排序 同一列。 例如
Column1 |
---|
Value1 value2 |
NSPP02 2111 |
NSPP02 2110 |
NSPP01 2111 |
我想先按value2再按value1排序。 按值 2 升序,然后按值 1 升序。
我使用聚合如下:
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 升序,然后按值 1 升序。
学期栏的示例序列如下:
- 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
我不确定您的理由或列背后的逻辑,但理想情况下,您将它们作为 2 个单独的列。它使这样的事情变得更容易。
话虽如此,我认为有两种方法可以解决这个问题。
如果您的 2 个值的长度永远不会改变 i.e.the 值 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 函数的功能。有一些不错的细节
您实际上可以先拆分学期列,然后 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