Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Joins

Newbie
 
Join Date: Aug 2009
Posts: 6
#1: Oct 9 '09
I've got two tables, customers and license, i am trying to run a query that will get the customer and license data, the two tables both have a matching customer ID. One customer ID can be in the license db several times, i.e. for trial and suspended licenses.

I want to be able to get the customer details with the most recent license aswell, i.e. get the license that has the latest expiry date.

Thanks,
James

ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 124
#2: Oct 12 '09

re: Using Joins


It would be better if u give table schema..

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM
  2. customer ct LEFT JOIN license lt
  3. ON ct.customerID=lt.customerID
  4. ORDER BY lt.ExpiryDate DESC
nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 33
#3: 2 Weeks Ago

re: Using Joins


You can even try this(I have given 2 queries)

Expand|Select|Wrap|Line Numbers
  1. SET DATEFORMAT dmy
  2. declare @tblcust table(custid int, cust varchar(20))
  3. insert into @tblcust select 1,'cust1' union all select 2,'cust2' union all
  4. select 3,'cust3' union all select 4,'cust4' union all select 5,'cust5'
  5.  
  6. declare @tblLisence table(custid int, lisensekey varchar(20),expirydate datetime)
  7. insert into @tblLisence select 1,'lisensekey1','01/01/2009' union all select 1,'lisensekey2','01/02/2009' union all
  8. select 2,'lisensekey3','01/03/2009' union all select 5,'lisensekey4','01/04/2009' union all select 5,'lisensekey5','01/05/2009'
  9.  
  10.  
  11. 1) select c.*,l.* from @tblcust c , @tblLisence l where c.custid  = l.custid order by l.expirydate desc
  12.  
  13. 2) select c.*,l.* from @tblcust c inner join @tblLisence l on c.custid  = l.custid order by l.expirydate desc
Reply