*/ Bookings Invoiced within the last 30 days - Payment details included */
SELECT v.booking_no AS [Booking #],
v.showname AS [Showname],
v.organizationv6 AS [Organization],
s.salesperson_name AS [SalesPerson],
Cast(v.inv_date AS DATE) AS [Invoice Date],
v.invoice_no AS [Invoice #],
Cast(v.price_quoted - v.tax1 - v.tax2 AS decimal (14,2)) as [Before Tax Amount],
Cast(v.tax1 AS DECIMAL(9, 2)) AS [Tax 1],
Cast(v.tax2 AS DECIMAL(9, 2)) AS [Tax 2],
Cast(v.price_quoted AS DECIMAL(14, 2)) AS [Price Quoted],
(SELECT TOP 1 P.DateF
FROM tblPayment P
WHERE P.invoice_no = I.Invoice_cred_no
ORDER BY DateF DESC) AS [Payment Date],
CASE
WHEN i.Inv_cred_note = 0 THEN CAST(
(SELECT SUM(P.amount)
FROM tblPayment P
WHERE P.invoice_no = i.invoice_cred_no) AS DECIMAL(9,2))
ELSE 0
END AS [Payment Amount]
FROM vwbookandhist v
LEFT OUTER JOIN tblsalesper S
ON s.salesperson_code = v.salesperson
LEFT OUTER JOIN tblinvhead I on I.Invoice_cred_no = v.invoice_no
WHERE Inv_date >= (GETDATE() - 30)
ORDER BY v.OrganizationV6
/*Bookings invoiced since @StartDate, Payment details included*/
DECLARE @StartDate AS VARCHAR(20)
SET @StartDate = 'Jan 1 2019'
SELECT v.booking_no AS [Booking #],
v.showname AS [Showname],
v.organizationv6 AS [Organization],
s.salesperson_name AS [SalesPerson],
Cast(v.inv_date AS DATE) AS [Invoice Date],
v.invoice_no AS [Invoice #],
Cast(v.price_quoted - v.tax1 - v.tax2 AS decimal (14,2)) as [Before Tax Amount],
Cast(v.tax1 AS DECIMAL(9, 2)) AS [Tax 1],
Cast(v.tax2 AS DECIMAL(9, 2)) AS [Tax 2],
Cast(v.price_quoted AS DECIMAL(14, 2)) AS [Price Quoted],
(SELECT TOP 1 P.DateF
FROM tblPayment P
WHERE P.invoice_no = I.Invoice_cred_no
ORDER BY DateF DESC) AS [Payment Date],
CASE
WHEN i.Inv_cred_note = 0 THEN CAST(
(SELECT SUM(P.amount)
FROM tblPayment P
WHERE P.invoice_no = i.invoice_cred_no) AS DECIMAL(9,2))
ELSE 0
END AS [Payment Amount]
FROM vwbookandhist v
LEFT OUTER JOIN tblsalesper S
ON s.salesperson_code = v.salesperson
LEFT OUTER JOIN tblinvhead I on I.Invoice_cred_no = v.invoice_no
WHERE Inv_date >= @StartDate
ORDER BY v.OrganizationV6