Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help

Author  Topic 

jrussell999
Starting Member

14 Posts

Posted - 2009-12-08 : 09:29:33
Currently I have the following query:

SELECT     TOP (100) PERCENT dbo.tblAccounts.AccountNumber, dbo.tblAccounts.AccountName, dbo.tblAccounts.Address1, dbo.tblAccounts.Address2, 
dbo.tblAccounts.City, dbo.tblAccounts.State, dbo.tblAccounts.PostalCode, dbo.tblAccounts.Country, dbo.tblReps.RepName, dbo.tblAccounts.AccountID,
dbo.tblAccounts.AccountType, dbo.tblAccounts.InitialContact, dbo.tblAccounts.IsVendor, dbo.tblAccounts.IsSubcontractor,
dbo.tblAccounts.PrimaryPhoneNumber, dbo.tblAccounts.PrimaryFaxNumber, dbo.tblAccounts.PrimaryContactNumber,
dbo.tblAccounts.ARCustomerNumber, dbo.tblAltBillTo.Name, dbo.tblAltBillTo.Address1 AS Expr1, dbo.tblAltBillTo.Address2 AS Expr2,
dbo.tblAltBillTo.City AS Expr3, dbo.tblAltBillTo.State AS Expr4, dbo.tblAltBillTo.PostalCode AS Expr5, dbo.tblAltBillTo.ARCustomerNumber AS Expr7,
dbo.tblAccounts.SalesTaxCode, dbo.tblAccounts.TaxExemptID, dbo.tblAccounts.TaxExemptCategory, COALESCE (A.DateOpened, 0) AS Expr6
FROM dbo.tblAccounts INNER JOIN
dbo.tblAltBillTo ON dbo.tblAccounts.AccountNumber = dbo.tblAltBillTo.AccountNumber INNER JOIN
dbo.tblReps ON dbo.tblAccounts.RepNumber = dbo.tblReps.RepNumber LEFT OUTER JOIN
(SELECT MAX(DateOpened) AS DateOpened, AccountNumber
FROM dbo.tblServiceOrders
GROUP BY AccountNumber) AS A ON dbo.tblAccounts.AccountNumber = A.AccountNumber
ORDER BY dbo.tblAccounts.AccountNumber



The current query returns all accounts that are:
in tblAccounts AND tblAltBillTo AND tblReps (along with all of the max service order dates that go with these records).

Right?

What I want is:

ALL max service order dates with ALL Accounts (where AccountNumbers match) along with any matching info from BillTo and Reps tables.

ALL max service order dates OUTER JOIN with ALL Accounts LEFT OUTER JOIN BillTo (or nothing) and...?

Could someone help me out with this?

jrussell999
Starting Member

14 Posts

Posted - 2009-12-08 : 09:38:57
On second thought, I think what I need is:

ALL max service order dates WITH ANY MATCHING Accounts, BillTo, OR Reps info (where AccountNumbers match)

So it's not limited to accounts that appear in all of (Accounts, BillTo, Reps), but instead any of (Accounts, BillTo, Reps).

that is:
ALL max service order dates LEFT OUTER JOIN Accounts, BillTo, and Reps info
right? How would I express that properly?
Go to Top of Page
   

- Advertisement -