SQL PIVOT日期未按正确顺序排列

时间:2018-02-15 09:04:09

标签: sql sql-server

我正在尝试将上个月的最后一天记录到当月的最后一天,但代码如下

checkCards()

以下是输出

    SELECT [DAY],
       [N-1 HSD],
       [N-2 HSD],
       [N-6 HSD],
       DIP,
       [STOCK LTR],
       [PURRCHASE LTR],
       [STOCK LTR] + [PURRCHASE LTR] AS [Total Ltr],
       Isnull([TESTING], 0) AS Testing,
       0 AS [Sales As Reading],
       0 AS [Sales As Dip],
       0 AS [Diff Ltr],

  (SELECT TOP (1) _RATE
   FROM _PRODUCTRATE
   WHERE _ISDELETED = N'1'
     AND _PRODUCTID_PK = N'154eeb16-0c5c-44e4-b5cc-b0f30a22098f'
     AND _DATE <= _RATEDATE
   ORDER BY _DATE DESC) AS [Rate Rs],
       0 AS [Amount Rs]
FROM
  (SELECT TOP (100) PERCENT Day(DBO._DSRENTRY._DSRENTRYDATE) AS [Day],
                            DBO._DSRENTRYDET._CLOSING,
                            DBO._DSRENTRY._DIPSCALE AS DIP,
                            DBO._DSRENTRY._DIP AS [Stock Ltr],
                            DBO._DSRENTRY._TESTINGQTY AS [Testing],
                            DBO._NOZZLE._NOZZLENAME,

     (SELECT Isnull(Sum(_QTY), 0) AS Expr1
      FROM DBO._TANKVACCANT
      WHERE (_ISDELETED = N'1')
        AND (_TANKID_PK = N'88e31d8c-acf5-4c09-a91f-28577c66d070')
        AND (_TANKVACCANTDATE = _DSRENTRYDATE)
        AND (_COMPANYID_PK = N'747c28ad-2924-4456-91fe-6f4f7b0db3fd')
        AND (_WORKGROUPNAME = N'SRV')) AS [Purrchase Ltr],
                            DBO._DSRENTRY._DSRENTRYDATE AS _RATEDATE
   FROM DBO._NOZZLE
   RIGHT OUTER JOIN DBO._DSRENTRYDET ON DBO._NOZZLE._NOZZLEID_PK = DBO._DSRENTRYDET._NOZZLEID_PK
   RIGHT OUTER JOIN DBO._DSRENTRY ON DBO._DSRENTRYDET._DSRENTRYID_PK = DBO._DSRENTRY._DSRENTRYID_PK
   WHERE (DBO._DSRENTRYDET._ISDELETED = N'1')
     AND (DBO._DSRENTRY._COMPANYID_PK = N'747c28ad-2924-4456-91fe-6f4f7b0db3fd')
     AND (DBO._DSRENTRYDET._COMPANYID_PK = N'747c28ad-2924-4456-91fe-6f4f7b0db3fd')
     AND (DBO._NOZZLE._COMPANYID_PK = N'747c28ad-2924-4456-91fe-6f4f7b0db3fd')
     AND (DBO._DSRENTRY._WORKGROUPNAME = N'SRV')
     AND (DBO._DSRENTRYDET._WORKGROUPNAME = N'SRV')
     AND (DBO._NOZZLE._WORKGROUPNAME = N'SRV')
     AND (DBO._DSRENTRY._ISDELETED = N'1')
     AND (DBO._NOZZLE._ISDELETED = N'1')
     AND (DBO._DSRENTRY._DSRENTRYDATE >= CONVERT(DATETIME, '31.08.2017 00:00:00', 105))
     AND (DBO._DSRENTRY._DSRENTRYDATE <= CONVERT(DATETIME, '30.09.2017 23:59:59', 105))
     AND (DBO._DSRENTRY._TANKID_PK = N'88e31d8c-acf5-4c09-a91f-28577c66d070')
   ORDER BY DBO._DSRENTRY._DSRENTRYDATE) AS DERIVEDTBL_1 PIVOT(Sum(_CLOSING)
                                                               FOR _NOZZLENAME IN ([N-1 HSD], [N-2 HSD], [N-6 HSD])) AS PVTTABLE

正如在输出中显示的那样,DAY N-1 HSD N-2 HSD N-6 HSD DIP STOCK LTR PURRCHASE LTR Total Ltr Testing Sales As Reading Sales As Dip Diff Ltr Rate Rs Amount Rs 1 1886247.00 1139460.00 391836.00 140.00 15319.00 0.00 15319.00 10.00 0 0 0 59.86 0 ......... ......... ......... 29 1909720.00 1147850.00 397467.00 102.40 10473.00 0.00 10473.00 10.00 0 0 0 61.57 0 30 1910934.00 1148180.00 397467.00 90.60 8932.00 6000.00 14932.00 10.00 0 0 0 61.65 0 31 1885620.00 1139191.00 391788.00 70.00 6310.00 10000.00 16310.00 10.00 0 0 0 59.78 0 的最后一个节目显示在底部,但这应该是第一个,

如果我将31 1885620.00 1139191.00 391788.00 70.00 6310.00 10000.00 16310.00 10.00 0 0 0 59.78 0更改为Day(DBO._DSRENTRY._DSRENTRYDATE),那么记录会按正确的顺序进行,但它会给我完整的日期和时间。 如下所示

DBO._DSRENTRY._DSRENTRYDATE

但我只需要一天没有完整的日期时间,

我是如何存档的。

1 个答案:

答案 0 :(得分:1)

使用htmlCode = htmlCode.replace(/<([^ >]+)[^>]*>/ig,'<$1>'); 有助于订购输出。请将它添加到select子句中,并按相同的列排序以获取已排序的输出。