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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Newbie SQL question... Please help :)

Author  Topic 

bobbyGratton
Starting Member

1 Post

Posted - 2009-06-01 : 13:12:53
Using MS SQL Server 2005.


I need help formulating a SQL query,

I have a table called "leads" and I need to select a particular group of records from.

Here's what i need:

-- Select all records that have the value of '5' in the "type" field (int), this is easy even for a noob :)

-- Also, I need to grab only uniques by "email" field. (I'm expecting that multiple groups of 2 or 3 records will have the same email address)

-- But, I need to grab only the LAST record to go in by email address. I have a date field called "created"


Thanks a lot for your help guys! I appreciate it.

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-01 : 13:27:52
try this

declare @test table ( id int identity(1,1), Name varchar(30),[type] int, email varchar(30),created datetime )
insert into @test
select 'Peter',5,'abc@zzz.com','5/1/2009' union all
select 'John',6,'rty@uuu.com','5/1/2009' union all
select 'Kelvin',5,'abc@zzz.com','6/1/2009'


select t.name,t.email
from (
select row_number() over( partition by [type] order by created desc ) as sno ,email,name
from @test
where [type] = '5')t
where t.sno = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-01 : 13:50:21
if sql 2000 or earlier

SELECT t.*
FROM table t
INNER JOIN (SELECT email,MAX(created) AS last
FROM table
WHERE t.type=5
GROUP BY email)t1
ON t1.email=t.email
AND t1.last=t.created


Go to Top of Page
   

- Advertisement -