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 |
|
MikaelJ
Starting Member
2 Posts |
Posted - 2003-03-27 : 05:26:47
|
| Hello,I need some help with a simple Select statement.First my query:SELECT CONTACT_NAME, CUSTOMER_NOFROM CONTACTJOIN CUSTOMERON CONTACT.OBJ_NO = CUSTOMER.OBJ_NO ORDER BY CUSTOMER_NO, CONTACT_NAMEResult:CONTACT_NAME CUSTOMER_NO ---------------------------------------- ----------- Elisabeth Moen 100NR 2 100Annika Högström 10000NR 2 10000Ivan Feldborg 1014Karin H. 1014Monica Källberg 1015Agneta Rogström 1018Anneli Malmhed 1018Ingrid Dahlström 1018Lena Bäcklin 1018Bernt Linström 1020Mats Österlund 1020P-O Karlström 1020Roger Vikbom 1020Elisabet Gustavsson 1022Kristina Harsbo 1022Siv Pettersson 1022and so on...I want to write a select statement that brings me this result:CONTACT_NAME CUSTOMER_NO ---------------------------------------- ----------- Elisabeth Moen 100Annika Högström 10000Ivan Feldborg 1014Monica Källberg 1015Agneta Rogström 1018Bernt Linström 1020Elisabet Gustavsson 1022I want the first CONTACT_NAME in the tabel for each CUSTOMER_NO and exclude all the other contact names for that specific Customer_no.Thanks for helping an amateur! |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-03-27 : 05:43:14
|
| Try this:SELECT Sub1.CONTACT_NAME, CUSTOMER_NO FROM CUSTOMER INNER JOIN (SELECT OBJ_NO, MIN(CONTACT_NAME) AS CONTACT_NAMEFROM CONTACT GROUP BY OBJ_NO) AS Sub1ON Sub1.OBJ_NO = CUSTOMER.OBJ_NOORDER BY CUSTOMER_NO, Sub1.CONTACT_NAME To return the first contact name for each customer, I have used the MIN() function, which will return the first entry alphabetically.OS |
 |
|
|
MikaelJ
Starting Member
2 Posts |
Posted - 2003-03-27 : 05:59:52
|
| Thank you mohdowais!Works just fine! Super!(I´ve tried earlier with the MIN-function without success...)/Mikael |
 |
|
|
|
|
|