SQL计算列基于其他计算列

时间:2017-03-03 12:41:01

标签: sql sql-server-2008-r2

我有一张包含发票清单的表格。在一个查询中,我根据发票条款计算发票到期日期(7天/ 30天等)

我有以下布局:

InvoiceNr | InvoiceDate | INVOICEDUEDATE

我想现在添加一个额外的列,根据当前日期将发票放入老化桶中。例如:

InvoiceNr | InvoiceDate | InvoiceDueDate |老化

但似乎我无法根据InvoiceDueDate列创建额外的列,因为那已经是组合列。因此,我如何创建一个可以计算和计算duedate的查询,并计算该发票的老化。

基本发票表看起来像这样:

Customer    Name    InvoiceTerms Description        Invoice        InvoiceDate  Value   InvoiceBal1
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050033 2015-11-06  0.00    0.00
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050034 2015-11-06  550.00  0.00
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050037 2015-11-09  362.18  0.00

通过以下查询,我添加了发票到期日

    SELECT     dbo.ArInvoice.Customer, dbo.ArCustomer.Name, dbo.ArCustomer.CustomerOnHold, dbo.ArCustomer.CreditStatus, dbo.ArCustomer.TermsCode AS CustomerTerms, 
                      dbo.ArInvoice.TermsCode AS InvoiceTerms, dbo.TblArTerms.Description, dbo.ArInvoice.Invoice, CAST(dbo.ArInvoice.InvoiceDate AS date) AS InvoiceDate, 
                      CAST(CASE ArInvoice.TermsCode WHEN '00' THEN CAST(dbo.ArInvoice.InvoiceDate AS date) WHEN '22' THEN dateadd(month, datediff(month, 0, DateAdd(month, MONTH(InvoiceDate), DateAdd(Year,
                       YEAR(InvoiceDate) - 1900, 0) + 1)), 0) - 1 WHEN '24' THEN dateadd(month, datediff(month, 0, DateAdd(month, MONTH(InvoiceDate) + 1, DateAdd(Year, YEAR(InvoiceDate) - 1900, 0) + 1)), 0) 
                      - 1 WHEN '28' THEN dateadd(month, datediff(month, 0, DateAdd(month, MONTH(InvoiceDate) + 2, DateAdd(Year, YEAR(InvoiceDate) - 1900, 0) + 1)), 0) 
                      - 1 ELSE InvoiceDate + TblArTerms.DueDays + TblArTerms.InvDayOfMonth END AS date) AS InvoiceDueDate, dbo.ArInvoice.CurrencyValue, dbo.ArInvoice.InvoiceBal1
FROM         dbo.ArCustomer INNER JOIN
                      dbo.ArInvoice ON dbo.ArCustomer.Customer = dbo.ArInvoice.Customer INNER JOIN
                      dbo.TblArTerms ON dbo.ArInvoice.TermsCode = dbo.TblArTerms.TermsCode

哪个给了我

Customer    Name    InvoiceTerms Description        Invoice        InvoiceDate DueDate    Value   InvoiceBal1
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050033 2015-11-06  2015-11-06  0.00    0.00
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050034 2015-11-06  2015-11-06  550.00  0.00
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050037 2015-11-09  2015-11-09  362.18  0.00

所以我现在需要的是一个额外的列,它接受invoiceDueDate, 检查getdate()和发票天数之间的差异是否大于30,那么它应该说是否过期。

例如:

Customer    Name    InvoiceTerms Description        Invoice        InvoiceDate DueDate    Value   InvoiceBal1   Ageing
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050033 2015-11-06  2015-11-06  0.00    0.00      OVERDUE
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050034 2015-11-06  2015-11-06  550.00  0.00      OVERDUE
    0000    CASH A/C        00  CASH ONLY ACCOUNT   000000000050037 2015-11-09  2015-11-09  362.18  0.00      OVERDUE

目前我能找到的唯一方法是创建两个视图。 第一个是invoiceduedate,第二个是衰老。 但这感觉有点矫枉过正?那么试图找到一种方法在一个查询中完成所有操作?

0 个答案:

没有答案