从昨天获得日期的结果

时间:2018-11-18 21:20:24

标签: sql-server

我正在尝试对所有没有销售订单的发票进行理算,并将其显示为SQL查询。日期应为昨天输入的发票。挑战在于结果似乎是静态的,不会改变

SELECT TOP (100) PERCENT CONVERT(varchar, DATEADD(dd, - 1, GETDATE()), 103) AS Date,
                         'Invoices w/o SO' AS Type,
                         COUNT(dbo.Invoice.InvoiceID) AS Orders,
                         COUNT(dbo.Invoice.InvoiceID) AS Chairs, 
                         ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST] 
FROM dbo.Invoice INNER JOIN dbo.Customers 
     ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID 
     INNER JOIN dbo.InvoiceDetails 
     ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo = 'TRIAL CHAIR') 
      OR
      (dbo.InvoiceDetails.ItemNo = 'STORAGE') 
      OR
      (dbo.InvoiceDetails.ItemNo = 'RSTF01') 
      OR
      (dbo.InvoiceDetails.ItemNo = 'FRDMC01') 
      AND 
      (dbo.Invoice.CredInvoiceNo IS NULL) 
      AND 
      (dbo.Invoice.EntryDate >= CONVERT(char(8), DATEADD(dd, - 1, GETDATE()), 112)) 
      AND 
      (dbo.Customers.CustomerID <> 187)

屏幕截图

1 个答案:

答案 0 :(得分:0)

谢谢大家。 我更改了查询并成功了!

SELECT
  CONVERT(varchar, DATEADD(dd, -1, GETDATE()), 103) AS Date,
  'Invoices w/o SO' AS Type,
  COUNT(dbo.Invoice.InvoiceID) AS Orders,
  COUNT(dbo.Invoice.InvoiceID) AS Chairs,
  ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]
FROM dbo.Invoice
INNER JOIN dbo.Customers
  ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID
INNER JOIN dbo.InvoiceDetails
  ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo IN ('TRIAL CHAIR', 'STORAGE', 'RSTF01', 'FRDMC01'))
AND (dbo.Invoice.CredInvoiceNo IS NULL)
AND (dbo.Customers.CustomerID <> 187)
AND (DATEDIFF(DAY, dbo.Invoice.EntryDate, GETDATE()) = 1)