t-sql Order Union语句按字母顺序与子组

时间:2017-09-21 13:48:01

标签: sql-server tsql

我真的希望我只是大脑冻结。我有3个工会的查询。查询1检索学校,查询2检索学校内的学院,查询3检索每个学院内的路径。我希望最终排序的产品看起来像这样:

学校   学院A.     途径1     途径2   学院B     途径1   学院C.     途径1     途径2     途径3

我的查询是这样做的;但我希望学院按字母顺序排列,这就是我在努力的地方。我忽略了一些明显的东西吗?

这是我的疑问:

Declare @schoolID int = 76

Select  l.schoolID      schoolID,
        null            academyID,
        null            pathwayID,
        l.locationName  targetName, 
        cast((1 - sum(ttlDaysMissed) / sum(ttlSchoolDays)) * 100 as decimal(5,1)) pct,
        1               srt1
From    cacheAttendanceAOLStudents aas
        Join dimLocation l on l.schoolID = aas.schoolID 
Where   aas.schoolID = @schoolID
Group by l.schoolID, l.locationName

UNION 
Select  aas.schoolID,
        a.aolAcademyID,
        null,
        a.academyName,
        cast((1 - sum(ttlDaysMissed) / sum(ttlSchoolDays)) * 100 as decimal(5,1)) pct,
        2
From    cacheAttendanceAOLStudents aas
        Join aolPathwayLocations pl on pl.aolPathwayLocationID = aas.aolPathwayLocationID
        Join aolAcademies a on a.aolAcademyID = pl.aolAcademyID
Where   aas.schoolID = @schoolID
Group by aas.schoolID, a.aolAcademyID, a.academyName

UNION 

Select  aas.schoolID,
        pl.aolAcademyID,
        pl.aolPathwayID,
        p.academyPathway,
        cast((1 - sum(ttlDaysMissed) / sum(ttlSchoolDays)) * 100 as decimal(5,1)) pct,
        2
From    cacheAttendanceAOLStudents aas
        Join aolPathwayLocations pl on pl.aolPathwayLocationID = as.aolPathwayLocationID
        Join aolPathways p on p.aolPathwayID = pl.aolPathwayID
Where   aas.schoolID = @schoolID
Group by aas.schoolID, pl.aolAcademyID, pl.aolPathwayID, p.academyPathway
order by srt1

这是一个运行以提供结果的查询:

Select 76 schoolID, null academyID, null pathwayID, 'Doss High' targetName, 91.2 pct, 1 srt1
UNION 
Select 76, 24, null, 'Academy Not Identified', 90.6, 2
UNION 
Select 76, 11, null, 'Freshman Academy', 93.4, 2
UNION 
Select 76, 24, 55, 'Pathway Not Identified', 90.6, 2
UNION 
Select 76, 11, 55, 'Pathway Not Identified', 93.4, 2
order by srt1

这就是完成的结果集的样子......如果'targetNames'按正确的字母顺序排列,那将是完美的......

schoolID    academyID   pathwayID   targetName              pct    srt1
76          NULL        NULL        Doss High               91.2    1
76          11          NULL        Freshman Academy        93.4    2
76          11          55          Pathway Not Identified  93.4    2
76          24          NULL        Academy Not Identified  90.6    2
76          24          55          Pathway Not Identified  90.6    2

1 个答案:

答案 0 :(得分:1)

您只需要ORDER BY targetname而不是srt1

Select 76 schoolID, null academyID, null pathwayID, 'Doss High' targetName, 91.2 pct, 1 srt1
UNION 
Select 76, 24, null, 'Academy Not Identified', 90.6, 2
UNION 
Select 76, 11, null, 'Freshman Academy', 93.4, 2
UNION 
Select 76, 24, 55, 'Pathway Not Identified', 90.6, 2
UNION 
Select 76, 11, 55, 'Pathway Not Identified', 93.4, 2
order by targetName;

联合末尾的ORDER BY子句将按所有联合结果集排序。

demo

'targetNames'将按正确的字母顺序排列:

| schoolID | academyID | pathwayID |             targetName |  pct | srt1 |
|----------|-----------|-----------|------------------------|------|------|
|       76 |        24 |    (null) | Academy Not Identified | 90.6 |    2 |
|       76 |    (null) |    (null) |              Doss High | 91.2 |    1 |
|       76 |        11 |    (null) |       Freshman Academy | 93.4 |    2 |
|       76 |        11 |        55 | Pathway Not Identified | 93.4 |    2 |
|       76 |        24 |        55 | Pathway Not Identified | 90.6 |    2 |