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.
| Author |
Topic |
|
Razzle00
Starting Member
35 Posts |
Posted - 2007-04-03 : 14:38:16
|
| Hi, How could I pull all the records from 1 table and only the record with the minimum value from the second table where the 2 tables id fields match?table 1 looks like this....id customer1 abc company2 xyz company3 pgj companytable 2 looks likeid salesdate1 01/01/20051 02/03/20061 03/05/20072 01/01/20043 12/31/20063 04/04/2003select c.id,c.customer,min(s.salesdate) from customer c inner join sales s on s.id=c.idresult needs to be 1 of each customer record from the first table and only the minimum value record for the salesdate of the sales table.1 abc company 01/01/20052 xyz company 01/01/20043 pgj company 04/04/2003Thanks,Razzle |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 14:46:01
|
| select t1.id, t1.customer, t2.msd as MinSalesDatefrom table1 as t1inner join (select id, min(salesdate) as msd from table2 group by id) as xon x.id = t1.idPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 14:48:43
|
| select t1.id, t1.customer, min(t2.salesdate) as MinSalesDatefrom table1 as t1inner join table2 as t2 on t2.id = t1.idgroup by t1.id, t1.customerPeter LarssonHelsingborg, Sweden |
 |
|
|
Hippi
Yak Posting Veteran
63 Posts |
Posted - 2007-04-04 : 19:24:58
|
| select id,Customer, (select min(Saledate) from Sales where Sales.Id=Customer.Id )from Customer |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-04 : 21:32:11
|
quote: Originally posted by Hippi select id,Customer, (select min(Saledate) from Sales where Sales.Id=Customer.Id )from Customer
The query will works but it is not efficient. use Peter's code KH |
 |
|
|
Razzle00
Starting Member
35 Posts |
Posted - 2007-04-13 : 14:23:29
|
| Thanks everyone! Great suggestions. I tried them all and have it working.Razzle |
 |
|
|
|
|
|
|
|