i need to do a view in sql that returns the latest invoice date for each company

Posted by dave haughton on Stack Overflow See other posts from Stack Overflow or by dave haughton
Published on 2010-06-14T15:45:45Z Indexed on 2010/06/14 15:52 UTC
Read the original article Hit count: 155

Filed under:
|

hi, i have a company table that is dbo.companies and has companyId as a column. I also have an invoice table that is dbo.invoices with invoicecompanyId column (which is the same as the companyId on the other table) and it also has a column called invoicedate. What i am mtrying to achieve is a view of each companyid with the corresponding latest invoice date for all the companies i have.

i have done the following but i dont know how to filter for the latest invoice, it returns all invoices from all companies and i need latest invoice for all companies

SELECT     TOP (100) PERCENT 
    'A' + 
    SUBSTRING('000000', 1, 6 - LEN(CAST(dbo.companies.companyId AS varchar(10)))) +
    CAST(dbo.companies.companyId AS varchar(10)) AS Client_ID, 
    dbo.invoices.invoiceDate AS S_Inv_Date
FROM dbo.invoices 
INNER JOIN dbo.companies 
ON dbo.invoices.invoiceCompanyId = dbo.companies.companyId
ORDER BY Client_ID

can you help please

ta

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server