Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Help eliminate dups in a view

Posted on 2004-03-29
Medium Priority
Last Modified: 2012-08-14
Hi I have a view that is pretty heavy and complicated... but for the most part it just joins a contacts table and an inventory table... the problem is that on person may own more than one coach, but in the resulting view I only want to see... lets say the most recent coach, do I have to create a view off of my inventory table that filters them out?
Question by:poulsborv
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 50

Expert Comment

ID: 10706982
it depends ...

post the ddl structures for the contacts and inventory tables (and any others)
and specify how they should be joined to get the "latest" coach..

LVL 15

Expert Comment

ID: 10707491
select contacts.*, inventory.* from contacts
inner join inventory on contacts.contactID = inventory.contactID
inner join (select max(uniqueID) uniqueID from inventory group by contactID, inventoryID) tmp
on tmp.inventoryID = inventory.inventoryID

In this query I'm assuming inventoryID is the code for each piece of inventory.  All coaches would have the same number, all of each item would have the same number.  This would get the most recent of each piece of inventory for each person.

Author Comment

ID: 10707777
CREATE VIEW dbo.vContactsAndCoaches
SELECT     dbo.Inventory.StockNo, dbo.Inventory.[Year], dbo.Inventory.Make, dbo.Inventory.Model, dbo.Inventory.ModelNo, dbo.Inventory.Loc,
                      dbo.Inventory.Retail, dbo.Inventory.SerialNo, dbo.Inventory.SoldDate, dbo.Inventory.Body, dbo.Locations.Location, dbo.LookingFor.MinPrice,
                      dbo.LookingFor.MaxPrice, dbo.LookingFor.Financing, dbo.States.StateAndAbr, dbo.Contacts.FirstName, dbo.Contacts.MiddleName,
                      dbo.Contacts.LastName, dbo.Contacts.Salutation, dbo.Contacts.SpouseFirstName, dbo.Contacts.SpouseMiddleName, dbo.Contacts.SpouseLastName,
                      dbo.Contacts.SpouseSalutation, dbo.Contacts.Address, dbo.Contacts.City, dbo.Contacts.State, dbo.Contacts.Zip, dbo.Contacts.Country,
                      dbo.Contacts.County, dbo.Contacts.Address2, dbo.Contacts.City2, dbo.Contacts.State2, dbo.Contacts.Zip2, dbo.Contacts.Country2,
                      dbo.Contacts.HomePhone, dbo.Contacts.WorkPhone, dbo.Contacts.CellPhone, dbo.Contacts.Fax, dbo.Contacts.PhoneExt, dbo.Contacts.Email,
                      dbo.Contacts.Email2, dbo.Contacts.DateEntered, dbo.Contacts.LastUpdated, dbo.Contacts.ReferredBy, dbo.Contacts.LocationID,
                      dbo.Contacts.LastResult, dbo.Contacts.MergeDate, dbo.Contacts.[TimeStamp], dbo.Contacts.Origin, dbo.Contacts.Birthday,
                      dbo.Contacts.SpouseBirthday, dbo.Contacts.Anniversary, dbo.Contacts.Picture, dbo.Contacts.InterestedinID, dbo.Contacts.Title, dbo.Contacts.Company,
                      dbo.Contacts.LastResults, dbo.Contacts.TradeIn, dbo.Contacts.Confirmed, dbo.Contacts.Salesperson, dbo.Contacts.Salesperson2,
                      dbo.Contacts.ContactID, Users_1.FullName AS EnteredBy, Users_2.FullName AS UpdatedBy, dbo.Contacts.FullName AS ContactFullName,
                      Users_3.FullName AS SalespersonFull, dbo.Contacts.heard, dbo.Contacts.BC, Users_4.FullName AS SalespersonFull2
FROM         dbo.Users Users_3 RIGHT OUTER JOIN
                      dbo.Contacts LEFT OUTER JOIN
                      dbo.Users Users_4 ON dbo.Contacts.Salesperson2 = Users_4.UserID ON Users_3.UserID = dbo.Contacts.Salesperson LEFT OUTER JOIN
                      dbo.Users Users_2 ON dbo.Contacts.UpdatedBy = Users_2.UserID LEFT OUTER JOIN
                      dbo.Users Users_1 ON dbo.Contacts.EnteredBy = Users_1.UserID LEFT OUTER JOIN
                      dbo.States ON dbo.Contacts.State = dbo.States.StateAbr LEFT OUTER JOIN
                      dbo.LookingFor ON dbo.Contacts.ContactID = dbo.LookingFor.ContactID LEFT OUTER JOIN
                      dbo.Locations ON dbo.Contacts.LocationID = dbo.Locations.LocationID LEFT OUTER JOIN
                      dbo.Inventory ON dbo.Contacts.ContactID = dbo.Inventory.ContactID

This is the statement as it stands currently.. the stockno on the vehicle is the primaryId for the inventory table
LVL 50

Accepted Solution

Lowfatspread earned 750 total points
ID: 10711317
This addition to the view may do it

CREATE VIEW dbo.vContactsAndCoaches
SELECT     I.StockNo, I.[Year], I.Make, I.Model, I.ModelNo, I.Loc,
                      I.Retail, I.SerialNo, I.SoldDate, I.Body, L.Location, Look.MinPrice,
                      Look.MaxPrice, Look.Financing, S.StateAndAbr, C.FirstName, C.MiddleName,
                      C.LastName, C.Salutation, C.SpouseFirstName, C.SpouseMiddleName, C.SpouseLastName,
                      C.SpouseSalutation, C.Address, C.City, C.State, C.Zip, C.Country,
                      C.County, C.Address2, C.City2, C.State2, C.Zip2, C.Country2,
                      C.HomePhone, C.WorkPhone, C.CellPhone, C.Fax, C.PhoneExt, C.Email,
                      C.Email2, C.DateEntered, C.LastUpdated, C.ReferredBy, C.LocationID,
                      C.LastResult, C.MergeDate, C.[TimeStamp], C.Origin, C.Birthday,
                      C.SpouseBirthday, C.Anniversary, C.Picture, C.InterestedinID, C.Title, C.Company,
                      C.LastResults, C.TradeIn, C.Confirmed, C.Salesperson, C.Salesperson2,
                      C.ContactID, Users_1.FullName AS EnteredBy, Users_2.FullName AS UpdatedBy, C.FullName AS ContactFullName,
                      Users_3.FullName AS SalespersonFull, C.heard, C.BC, Users_4.FullName AS SalespersonFull2
FROM   dbo.Contacts as C
Left Outer Join  dbo.Users Users_3
  ON C.Salesperson  = Users_3.UserID
LEFT OUTER JOIN dbo.Users Users_4
  ON C.Salesperson2 = Users_4.UserID
LEFT OUTER JOIN dbo.Users Users_2
  ON C.UpdatedBy = Users_2.UserID
LEFT OUTER JOIN dbo.Users Users_1
  ON C.EnteredBy = Users_1.UserID
LEFT OUTER JOIN dbo.States as S
  ON C.State = S.StateAbr
LEFT OUTER JOIN dbo.LookingFor as look
  ON C.ContactID = Look.ContactID
LEFT OUTER JOIN dbo.Locations as L
  ON C.LocationID = L.LocationID
LEFT OUTER JOIN dbo.Inventory as I
  ON C.ContactID = I.ContactID
Where I.stockno = (select max(I1.stockno)
                     from dbo.Inventory as I1
                    Where I1.contactID=C.ContactID)

you didn't really explain how the latest vehicle should be selected.....
so i just picked the latest stockno for the contact.......

are you certain that all these joins need to be outer joins?

i'd certainly have though you'd only want to return contacts with inventory...    

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question