在SQL Server 2005中通过asc进行透视排序

时间:2018-02-06 04:28:32

标签: sql-server

这是我的疑问:

select 
    Sno, TopEmpName, HighLevel, TopEmpID, CH, ED, DIR, GM, AGM, BDM, SMM, LowEmpID
from
    (select 
         projectallocation.proAllocationID as Sno,
         temp.intro_name as TopEmpName,
         projectallocation.introtoplevelEmpid as TopEmpID,
         temper.csshortname as HighLevel, 
         companystructure.csshortname as Level,
         introducermaster.intro_name as LowEmpName,
         projectallocation.introlevelEmpid as LowEmpID 
     from 
         projectallocation
     inner join 
         dbo.IntroducerMaster on dbo.introducermaster.empid = projectallocation.introLevelEmpid 
     inner join 
         introducermaster as temp on temp.empiD = projectallocation.introtopLevelEmpid
     inner join 
         companyStructure on projectallocation.introleveID = companyStructure.HLevel  
     inner join 
         companystructure as temper on projectallocation.introtoplevelID = temper.Hlevel
     where 
         projectallocation.projectID = 1
     group by 
         IntroducerMaster.Intro_Name, temp.intro_name,
         companyStructure.CSShortName, projectallocation.proAllocationID,
         projectallocation.introlevelEmpid, projectallocation.introtoplevelEmpid,
         projectallocation.introtoplevelID, temper.csshortname
     order by 
         projectallocation.introtoplevelID asc
 ) b
 PIVOT
     (max(LowEmpName)
         for level in (CH, ED, DIR, GM, AGM, BDM, SMM)
     ) PVT

我在哪里放置Order by条款?

我想订购BY CH,ED,.... forpempname

1 个答案:

答案 0 :(得分:0)

  

ORDER BY子句在视图,内联函数中派生无效   表,子查询和公用表表达式,除非TOP,OFFSET   或者也指定了FOR XML。

显然,当您派生表和子查询时,您无法对其进行排序。

要应用ORDER BY,请将输出包装到SELECT内,然后应用您的ORDER BY条件,如下所示。

SELECT * 
FROM   (select Sno, 
               TopEmpName, 
               HighLevel, 
               TopEmpID, 
               CH, 
               ED, 
               DIR, 
               GM, 
               AGM, 
               BDM, 
               SMM, 
               LowEmpID 
        from   (select projectallocation.proAllocationID    as Sno, 
                       temp.intro_name                      as TopEmpName, 
                       projectallocation.introtoplevelEmpid as TopEmpID, 
                       temper.csshortname                   as HighLevel, 
                       companystructure.csshortname         as Level, 
                       introducermaster.intro_name          as LowEmpName, 
                       projectallocation.introlevelEmpid    as LowEmpID 
                from   projectallocation 
                       inner join dbo.IntroducerMaster 
                               on dbo.introducermaster.empid = 
                                  projectallocation.introLevelEmpid 
                       inner join introducermaster as temp 
                               on temp.empiD = 
                                  projectallocation.introtopLevelEmpid 
                       inner join companyStructure 
                               on projectallocation.introleveID = 
                                  companyStructure.HLevel 
                       inner join companystructure as temper 
                               on projectallocation.introtoplevelID = 
                                  temper.Hlevel 
                where  projectallocation.projectID = 1 
                group  by IntroducerMaster.Intro_Name, 
                          temp.intro_name, 
                          companyStructure.CSShortName, 
                          projectallocation.proAllocationID, 
                          projectallocation.introlevelEmpid, 
                          projectallocation.introtoplevelEmpid, 
                          projectallocation.introtoplevelID, 
                          temper.csshortname) b 
               PIVOT (Max(LowEmpName) 
                     for level in (CH, 
                                   ED, 
                                   DIR, 
                                   GM, 
                                   AGM, 
                                   BDM, 
                                   SMM) ) PVT) T 
ORDER  BY CH,ED