按选择的不同查询按日期排序

时间:2017-06-02 05:46:40

标签: sql sql-server

我从电子邮件正文的临时表中获取此查询的数据

 SELECT DISTINCT (CONVERT (nvarchar(10), a.ORIG_DT, 110)) as Ingestion_date,
            a.RECORD_TYPE_CD,
            a.INVOICE_TYPE, 
            COUNT (a.RECORD_TYPE_CD)    
 FROM @report_invoices a , @report_invoices b 
 WHERE  (CONVERT (nvarchar(10), a.ORIG_DT, 103)) = (CONVERT (nvarchar(10), b.ORIG_DT, 103)) 
       AND a.RECORD_TYPE_CD = b.RECORD_TYPE_CD 
       and a.INVOICE_TYPE = b.INVOICE_TYPE 
GROUP BY  (CONVERT (nvarchar(10), a.ORIG_DT, 110)) ,
          a.RECORD_TYPE_CD,
          a.INVOICE_TYPE 
ORDER BY Ingestion_date 

但订单托管日期有问题。

获取此订单

01-04-2017  ImportedHistory Iron Mountain uploaded  1
01-12-2017  PCard   Iron Mountain uploaded  1
01-13-2017  PCard   Iron Mountain uploaded  25
01-18-2017  ImportedHistory Iron Mountain uploaded  36
01-23-2017  ImportedHistory Iron Mountain uploaded  1
01-24-2017  ImportedHistory Iron Mountain uploaded  1
03-13-2017  PCard   Iron Mountain uploaded  1
05-16-2017  Invoice electronic uploads  1
12-05-2016  PCard   Iron Mountain uploaded  9
12-06-2016  PCard   Iron Mountain uploaded  961
12-06-2016  Serengeti   Iron Mountain uploaded  1
12-07-2016  PCard   Iron Mountain uploaded  196
12-08-2016  Invoice Iron Mountain uploaded  36
12-09-2016  Invoice Iron Mountain uploaded  289
12-14-2016  ImportedHistory Iron Mountain uploaded  64
12-15-2016  CheckRequest    electronic uploads  1
12-15-2016  ImportedHistory Iron Mountain uploaded  144
12-16-2016  ImportedHistory Iron Mountain uploaded  36
12-19-2016  CheckRequest    electronic uploads  81
12-19-2016  PCard   Iron Mountain uploaded  9
12-20-2016  CheckRequest    electronic uploads  9
12-20-2016  Invoice Iron Mountain uploaded  1
12-20-2016  PCard   Iron Mountain uploaded  484
12-21-2016  CheckRequest    electronic uploads  1
12-21-2016  ImportedHistory Iron Mountain uploaded  9
12-21-2016  PCard   Iron Mountain uploaded  64
12-22-2016  CheckRequest    electronic uploads  36
12-22-2016  PCard   Iron Mountain uploaded  1
12-23-2016  CheckRequest    electronic uploads  4

需要更正订单

3 个答案:

答案 0 :(得分:1)

由于您无法按不同查询中不存在的列进行排序,因此您需要执行以下操作:

SELECT CONVERT (nvarchar(10), ORIG_DT, 110) as Ingestion_date,RECORD_TYPE_CD,INVOICE_TYPE, RECORD_COUNT   
from ( 
    select
    convert(date, a.ORIG_DT) as ORIG_DT,a.RECORD_TYPE_CD,a.INVOICE_TYPE, COUNT (a.RECORD_TYPE_CD) as RECORD_COUNT
    @report_invoices a , @report_invoices b 
    where CONVERT (date, a.ORIG_DT) = CONVERT (date, b.ORIG_DT, 103) AND a.RECORD_TYPE_CD = b.RECORD_TYPE_CD 
    and a.INVOICE_TYPE = b.INVOICE_TYPE GROUP BY convert(date, a.ORIG_DT), a.RECORD_TYPE_CD, a.INVOICE_TYPE 
) X
ORDER BY ORIG_DT 

答案 1 :(得分:1)

您不需要distinct关键字,因为您已在所有列上使用group by,但选择列表中的count除外。
这意味着您可以在order by子句中使用a.ORIG_DT

 SELECT CONVERT (nvarchar(10), a.ORIG_DT, 110) as Ingestion_date,
        a.RECORD_TYPE_CD,
        a.INVOICE_TYPE, 
        COUNT (a.RECORD_TYPE_CD)    
 FROM @report_invoices a , @report_invoices b 
 WHERE  (CONVERT (nvarchar(10), a.ORIG_DT, 103)) = (CONVERT (nvarchar(10), b.ORIG_DT, 103)) 
       AND a.RECORD_TYPE_CD = b.RECORD_TYPE_CD 
       and a.INVOICE_TYPE = b.INVOICE_TYPE 
GROUP BY  (CONVERT (nvarchar(10), a.ORIG_DT, 110)) ,
          a.RECORD_TYPE_CD,
          a.INVOICE_TYPE 
ORDER BY a.ORIG_DT

答案 2 :(得分:0)

同样在选择和顺序中使用相同的格式:

SELECT DISTINCT (CONVERT (nvarchar(10), a.ORIG_DT, 110)) as Ingestion_date,
        a.RECORD_TYPE_CD,
        a.INVOICE_TYPE, 
        COUNT (a.RECORD_TYPE_CD)    
FROM @report_invoices a , @report_invoices b 
WHERE  (CONVERT (nvarchar(10), a.ORIG_DT, 103)) = (CONVERT (nvarchar(10), 
 b.ORIG_DT, 103)) AND a.RECORD_TYPE_CD = b.RECORD_TYPE_CD 
 and a.INVOICE_TYPE = b.INVOICE_TYPE 
 GROUP BY  (CONVERT (nvarchar(10), a.ORIG_DT, 110)) ,
 a.RECORD_TYPE_CD,a.INVOICE_TYPE 
 ORDER BY (CONVERT (nvarchar(10), a.ORIG_DT, 110))