我有一個會計年度的開始日期和結束日期範圍:16年7月1日 - 包括我的查詢17年6月30日T-SQL查詢只開始日期和結束日期之間的發票
日期字段是可以在6/30/17之外的日期付款。
例子:
Company PayonDate
Bose 9/30/17 <---Outside the 6/30/17 date range and will not show on report. This date will pay in advance.
我想顯示的日期範圍內的最後支付日期:
例子:
Company PayonDate
Bose 4/1/17 <----Actual paid on date vs. pay on date in the future within the date range.
有沒有辦法做到這一點?
在此先感謝!
更新:
SELECT
ven.VendorID,
ven.Name AS [Vendor Name],
inv.ReferenceNumber AS [Ref. No],
inv.InvoiceNumber AS [Invoice No],
inv.Payment AS Payment,
SubMax.[Pay On Date]
FROM inv
INNER JOIN
(
SELECT MAX(ips.PayOnDateTime) AS [Pay On Date], VendorID
from inv
LEFT JOIN ips ON ips.SourceID = inv.SourceID AND ips.InvoiceID = inv.InvoiceID
Group by VendorID
) SubMax
on inv.VendorID = SubMax.VendorID
INNER JOIN ven ON inv.SourceID = ven.SourceID AND inv.VendorID = ven.VendorID
LEFT JOIN ips ON ips.SourceID = inv.SourceID AND ips.InvoiceID = inv.InvoiceID
WHERE FacilityID = 'HMC'
AND SubMax.[Pay On Date] BETWEEN @StartDate AND @EndDate
AND inv.ReferenceNumber LIKE '__-____'
ORDER BY ven.Name, SubMax.[Pay On Date]
類似於'SELECT MAX(PayonDate)FROM TABLE WHERE PayonDate> ='2016-07-01'AND PayonDate <'2017-07-01'' –
感謝Jacob。我在我的子查詢中實際上有這個。 – cmpmd2
PayonDate是什麼數據類型?我們可以在這裏幫助,但我們需要一些細節。這裏是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –