Hello all, I hope you can help me get the query results I need. I'm calculating project margin using the sum of invoices for a certain period (currently YTD), then subtracting the costs that were incurred up to and including the latest invoice date. I take the remainder and divide it by the sum of the invoices so it looks like:
(sum of invoices - costs incurred up to and including the last invoice date)/sum of invoices (e.g. 1000-400/1000 = 60%)
There may be several invoices generated during the period so, besides summing the invoices, I have to find the date of the most current invoice. I'm using OVER and PARTITION BY to get this date. I want to take that date and use it to retrieve the costs incurred either equal to or prior to the latest invoice date.
My query to get the sum of the invoices YTD and the latest invoice date returns: (InvoiceTotal, ProjectNo, LastInvoiceDate); this table already has unique values per row. My query to get the costs returns: (Costs, ProjectNo, CostTransDate); this table returns several rows per ProjectNo. I want to use the LastInvoiceDate to filter the results in the second dataset (e.g. CostTransDate <= LastInvoiceDate) and then group by ProjectNo to end up with one CostTotal per ProjectNo.
I want to use this in SSRS so it would be best if I can have one dataset returned. Then I can use the columns i need in my calculation. I'd like to have one dataset (ProjectNo, InvoiceTotal, CostTotal)
Any help is greatly appreciated.
Milissa Hartwell