MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 27th, 2004, 10:51 AM
adam123 adam123 is offline
adam
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Virginia
Posts: 2 adam123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation How to Retrieve first unique record

Refering to the SQL code below, I need it to return only the first record for each CO_CUSTOMER_CODE. I need to retrieve all of the other fields even if there are duplicates. Right now I get records returned like this:

12345 John Smith URL
12345 Jill Brown URL
12346 Fred Flinstone URL

And I want this:

12345 John Smith URL
12346 Fred Flinstone URL

Is there a way to do this in SQL Server?

SELECT DISTINCT
CONTACTS.CO_CUSTOMER_CODE As [Customer #],
CONTACTS.CO_CONTACT_NAME As [Name],
CONTACTS.CO_EMAIL As [Email]
FROM
CONTACTS
ORDER BY
CONTACTS.CO_CUSTOMER_CODE

Reply With Quote
  #2  
Old April 27th, 2004, 11:52 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
Quote:
I need it to return only the first record for each CO_CUSTOMER_CODE.


There is no concept of first in a RDBMS. You can only distinguish between records by the values in the different columns in the record.

If you wish to retrieve the record that was entered in the database at the earliest point of time, you need to store the date and time when the post was entered.

You can also get the record with max/min value (according to the collating sequence) of the email adress for each customer code.

Code:
SELECT
CONTACTS.CO_CUSTOMER_CODE As [Customer #],
CONTACTS.CO_CONTACT_NAME As Name,
CONTACTS.CO_EMAIL As Email
FROM
CONTACTS as c
where email in (select max(email) 
from contacts where co_customer_code = c.co_customer_code)
ORDER BY
CONTACTS.CO_CUSTOMER_CODE

Reply With Quote
  #3  
Old April 27th, 2004, 01:06 PM
adam123 adam123 is offline
adam
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Virginia
Posts: 2 adam123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What I'm trying to do is get back a 'distinct' contact for each customer record from the Contacts table. It doesn't matter which contact, but I want just one instead of multiple contacts returned. I am making a report to show the contact information for customers. If it helps the primary key is

CONTACTS.CO_CONTACT_CODE, CONTACTS.CO_CUSTOMER_CODE

Reply With Quote
  #4  
Old April 27th, 2004, 05:54 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
What DBMS?

give this a shot...
Code:
select * 
from   Contacts C
join   (select  distinct co_customer_code,
                max(email) as email,
                count(email) 
        from    Contacts
        group   by co_customer_code
        having  count(email) > 1) as A
on     C.co_customer_code = A.co_customer_code
and    C.email = A.email

..all though I'm curious why you need the code and name as the primary key? If you just used the code as the primary key this post would turn irrelevant...each code would then have it's own distinct contact...just curious.

EDIT: boogie's query looks like it would work as well.

Last edited by Username=NULL : April 27th, 2004 at 06:07 PM. Reason: just looked @ boogie's query...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > How to Retrieve first unique record


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway