Wednesday, March 7, 2012

JOIN Statement Help

I am very new to SQL and need to create a statement that will JOIN data from 3 tables into my datagrid. The following are the tables:
Table A: Compliance
- FinancialsID
-NetWorth
-DebtRatio
-WorkCapital

Table B: Financials
- FinancialsID
- cAssets
- TransDate
- CustomerID

Table C: CompanyInfo
- CustomerID
- Company
- Agent

I need to be able to display Company.CompanyInfo, NetWorth.Compliance, DebtRatio.Compliance, WorkCapital.Compliance in a datagrid and make sure that it ONLY displays the most current entry for the Company.

The Compliance table has a relationship to the Financials table through the FinancialsID field and the Financials table is related to the CompanyInfo table through the CustomerID field. The TransDate is a date field in the Financials table.

This seems extremely confusing to me, but I am sure its easier than what I am trying to make it.

Any help would be GREATLY appreciated.

Thanks
Garrettto start with :


SELECT Company.CompanyInfo, NetWorth.Compliance, DebtRatio.Compliance, WorkCapital.Compliance
FROM CompanyInfo ,Financials ,Compliance
WHERE companyInfo.customerid = Financials.customerid AND Financials.FinancialsID=Compliance.FinancialsID

now what do you mean by most current ? does it have anything to do with the date ?|||Thanks for the help on this.

There can be multiple financial records for each company. There is a date field in the Financials table named TransDate so that I can differentiate the records within a company.

The main purpose of my web app is to create a sort of 'PipeLine'. When a user logs into the page he/she is supposed to see the most current financials for each company in their respective pipeline.

For this reason, I need to ensure that only the most current record is returned for each company.

Any Ideas?-

Thanks|||yes but I dont understand what you mean by "current" do you mean today ? what is the condition for the date ? give technical details.|||you need to include the date in ur sql statement and order by date|||I have been able to JOIN the tables and display the correct fields in the datagrid, however, I have been unable to reconstruct the SQL statement so that only the LATEST financial records row is displayed for each Company.

Here is what I have:


SELECT CompanyInfo.Company, CompanyInfo.customerID, CompanyInfo.uname,
Compliance.FinancialsID, Compliance.NetWorth, Compliance.uname,
Compliance.DebtRatio, Compliance.WorkCapital FROM CompanyInfo INNER JOIN
Financials ON CompanyInfo.customerID = Financials.customerID INNER JOIN Compliance
ON Financials.FinancialsID = Compliance.FinancialsID ORDER BY TransDate DESC"

How do I simple return one record for each company and make sure that record is the most current by date (TransDate)?

Thanks|||Think I figured it out.

"SELECT CompanyInfo.Company, CompanyInfo.CustomerID, CompanyInfo.uname, " & _

"Compliance.FinancialsID, Compliance.NetWorth, Compliance.uname, " & _

"Compliance.DebtRatio, Financials.TransDate, Compliance.WorkCapital " & _

"FROM CompanyInfo " & _

"INNER JOIN Financials ON CompanyInfo.CustomerID = Financials.CustomerID " & _

"INNER JOIN Compliance ON Financials.FinancialsID = Compliance.FinancialsID " & _

"WHERE Financials.TransDate = (SELECT MAX(TRansDate) FROM Financials F1 " & _

"WHERE F1.CustomerID = Financials.CustomerID)"

Thanks for everyone's help.

No comments:

Post a Comment