如何使用SQL Server在数据透视表中使用Order by

时间:2014-10-11 15:22:21

标签: sql sql-server sql-server-2008

我正在使用SQL Server 2008,并在下面查询数据透视表,

SELECT [pay date], 
       [adm. fee], 
       [colleege dev.] 
FROM   (SELECT 
       CONVERT(VARCHAR(10), CONVERT(DATE, dbo._feepayment._issuedate, 106), 105) AS [Pay Date], 
       dbo._feehead._feeheadname AS aaa, 
       Isnull(dbo._feepaymentdet._amount, 0) AS _AMOUNT 
        FROM   dbo._feepaymentdet 
               LEFT OUTER JOIN dbo._feehead 
                            ON dbo._feepaymentdet._feeheadid = 
                               dbo._feehead._feeheadid 
               RIGHT OUTER JOIN dbo._feepayment 
                             ON dbo._feepaymentdet._feepaymentid = 
                                dbo._feepayment._feepaymentid 
        WHERE  ( dbo._feehead._isdeleted = N'1' ) 
               AND ( dbo._feepayment._isdeleted = N'1' ) 
               AND ( dbo._feepaymentdet._isdeleted = N'1' ) 
               AND dbo._feepayment._issuedate >= 
                   CONVERT(DATETIME, '01.09.2014 00:00:00', 105) 
               AND dbo._feepayment._issuedate <= 
                   CONVERT(DATETIME, '11.09.2014 23:59:59', 105)) AS s 
       PIVOT ( Sum(s._amount) 
             FOR s.aaa IN ( [ADM. FEE], 
                            [COLLEEGE DEV.] ) ) pvt 

以上查询返回以下记录,

Pay Date        ADM. FEE     COLLEEGE DEV.
09-09-2014      25.00        100.00
05-09-2014      75.00        300.00
01-09-2014      25.00        100.00
03-09-2014      75.00        310.00
02-09-2014      50.00        200.00

但如果我添加

,我希望付款日期高于记录ORDER BY
WHERE (dbo._FEEHEAD._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENT._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENTDET._ISDELETED = N'1')  
 AND dbo._FEEPAYMENT._ISSUEDATE >= CONVERT(DATETIME, '01.09.2014 00:00:00', 105) 
 AND dbo._FEEPAYMENT._ISSUEDATE <= CONVERT(DATETIME, '11.09.2014 23:59:59', 105)
 ORDER BY dbo._FEEPAYMENT._ISSUEDATE 

SQL给我错误:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries,
 and common table expressions, unless TOP or FOR XML is also specified.

我如何在上面的查询中添加ORDER BY。

1 个答案:

答案 0 :(得分:4)

如果我没错,请使用TOP 100 Percent

由于派生表不允许使用内部查询的BY子句

SELECT TOP 100 Percent [Pay Date], [ADM. FEE], [COLLEEGE DEV.] FROM 
 (SELECT Convert(varchar(10),CONVERT(date,dbo._FEEPAYMENT._ISSUEDATE,106),105) AS [Pay Date], 
 dbo._FEEHEAD._FEEHEADNAME AS aaa, ISNULL(dbo._FEEPAYMENTDET._AMOUNT ,0) AS _AMOUNT 
 FROM dbo._FEEPAYMENTDET LEFT OUTER JOIN dbo._FEEHEAD ON dbo._FEEPAYMENTDET._FEEHEADID 
 = dbo._FEEHEAD._FEEHEADID RIGHT OUTER JOIN dbo._FEEPAYMENT ON 
 dbo._FEEPAYMENTDET._FEEPAYMENTID = dbo._FEEPAYMENT._FEEPAYMENTID 
 WHERE (dbo._FEEHEAD._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENT._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENTDET._ISDELETED = N'1')  
 AND dbo._FEEPAYMENT._ISSUEDATE >= CONVERT(DATETIME, '01.09.2014 00:00:00', 105) 
 AND dbo._FEEPAYMENT._ISSUEDATE <= CONVERT(DATETIME, '11.09.2014 23:59:59', 105)) 
 as s PIVOT ( Sum(s._AMOUNT) FOR s.aaa IN ( [ADM. FEE], [COLLEEGE DEV.] ) ) pvt
Order BY pvt.[Pay Date]

没有TOP 100 Percent

SELECT [Pay Date], [ADM. FEE], [COLLEEGE DEV.] FROM 
 (SELECT Convert(varchar(10),CONVERT(date,dbo._FEEPAYMENT._ISSUEDATE,106),105) AS [Pay Date], 
 dbo._FEEHEAD._FEEHEADNAME AS aaa, ISNULL(dbo._FEEPAYMENTDET._AMOUNT ,0) AS _AMOUNT 
 FROM dbo._FEEPAYMENTDET LEFT OUTER JOIN dbo._FEEHEAD ON dbo._FEEPAYMENTDET._FEEHEADID 
 = dbo._FEEHEAD._FEEHEADID RIGHT OUTER JOIN dbo._FEEPAYMENT ON 
 dbo._FEEPAYMENTDET._FEEPAYMENTID = dbo._FEEPAYMENT._FEEPAYMENTID 
 WHERE (dbo._FEEHEAD._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENT._ISDELETED = N'1') 
 AND (dbo._FEEPAYMENTDET._ISDELETED = N'1')  
 AND dbo._FEEPAYMENT._ISSUEDATE >= CONVERT(DATETIME, '01.09.2014 00:00:00', 105) 
 AND dbo._FEEPAYMENT._ISSUEDATE <= CONVERT(DATETIME, '11.09.2014 23:59:59', 105)) 
 as s PIVOT ( Sum(s._AMOUNT) FOR s.aaa IN ( [ADM. FEE], [COLLEEGE DEV.] ) ) pvt
Order BY pvt.[Pay Date]