If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > selecting unique values problem

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-09, 03:59
niths_86 niths_86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 9
selecting unique values problem

Hi ,
I want to form a select query where i need to display the list of all names and their ids. Here if the name already exists i need to check whether if their date of birth and registration date is different.Only if they are different should i display them.
Table
First Name Last name DoB id Regis_Date
Kevin Patrick 25-05-1987 1 29-07-2009
Steve Prince 20-1-20009 2 30-07-2009
Kevin Patrick 25-05-1987 3 29-07-2009
Steve Prince 23-1-2009 4 29-07-2009

Output should be
Name id
Kevin Patrick 1
StevePrince 2
Steve Prince 4 (Since DOB is different)
please help me in forming the query.
Reply With Quote
  #2 (permalink)  
Old 07-31-09, 08:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Code:
SELECT t.firstname || t.lastname AS name
     , t.id
  FROM ( SELECT firstname
              , lastname
              , dob
              , regis_date
              , MIN(id) AS min_id
           FROM daTable
         GROUP
             BY firstname
              , lastname
              , dob
              , regis_date ) AS d
INNER
  JOIN daTable AS t
    ON t.firstname  = d.firstname
   AND t.lastname   = d.lastname 
   AND t.dob        = d.dob 
   AND t.regis_date = d.regis_date
   AND t.id         = d.min_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-03-09, 05:47
niths_86 niths_86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 9
wow .. thanks a lot ..let me try .
Reply With Quote
  #4 (permalink)  
Old 08-03-09, 11:17
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 58
Just back from vacation I'd simply do:

Code:

SELECT firstname || ' ' || lastname AS name,
       min(id)
FROM tablename
GROUP BY firstname, lastname, DOB, regis_date

Or am I missing something obvious here?

Last edited by JarlH; 08-03-09 at 11:20.
Reply With Quote
  #5 (permalink)  
Old 08-03-09, 11:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
your vacation wasn't long enough, you seem to remember some SQL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On